0

In Node.js is it possible to create a virtual folder if so how?

Some context for needing a virtual folder: Have a project in Node.js that is a middleware is listinning to port 400 for example so can make request to my endpoints as follow http://{IP}:400/?p={%22getWebhooks%22:true}. The port 400 is open to make external requests.

In the same server there is another middleware that is .NET/C# that is using port 80 and it only works inside the server (its blocked from the outside) This Project serves to integrates images among other data, those images are saved in folder under that .NET project where in the Node.js need's to have access to those images in the folder (to avoid having to ask for another port to be open to the outside/ public IP)

The Node.js project is in C:\nodeProject

http://{IP}:400/?p={%22getWebhooks%22:true}

The Images folder is in C:\inetpub\wwwroot\{.netProject}\back_office\resources

to avoid needing another open port for The IIS for example port 200 to serve the images http://{IP}:200/resources/images.jpg

wanted to serve the images using Node.js http://{IP}:400/resources/images.jpg

Not using express.js using the http module

var server = http.createServer(function (req, res) {

Don't know if node-static package can be use for this use-case...

Thanks for any information that points me in the right direction.

H.C
  • 565
  • 7
  • 28
  • It is unclear what you want. Add more details, folder structure and tell us what exactly you want to do with the virtual folder in node app. – Molda Jan 14 '20 at 16:33
  • Sorry Molda for the unclear text have edit the Question, can you tell me if its readable now. – H.C Jan 14 '20 at 17:04
  • 1
    Yes that's much better. In this case you can use node-static without creating any app just install it globaly then cd into back_office and run `static` command. See [docs](https://www.npmjs.com/package/node-static#example-usage) Alternatively use it in app `var file = new static.Server('C:\inetpub\wwwroot\{.netProject}\back_office\');` – Molda Jan 15 '20 at 08:17
  • Thanks @Molda was able as you sayed by command line: static -p 400 -a 0.0.0.0 the -p is the flag for the port for example port 400 and the flag -a for the ip in this case 'expose the server to your local network' – H.C Jan 15 '20 at 11:26

1 Answers1

0

As @Molda refered by using the package node-static solved my probleam but had to use inside my app as in the code bellow where query === null is condition to see if the request doesn't have paramter's then its a image to be serve other wise it a normal request to endpoint of my Node.js app middleware.

var staticNode = require('node-static');
var fileServer = new staticNode.Server('C:/inetpub/wwwroot/example/back_office/resources/');
var server = http.createServer(function (req, res) {
    let body = []
    req.on('error', (err) => {
            console.error(err);
    }).on('data', chunk => {
            body.push(chunk);
    }).on('end', () => {
        let bodyResponse;
        if (body.length > 0) {
            bodyResponse = JSON.parse(body);
            //console.log(body);
            console.log(bodyResponse);
        }

        //var page = url.parse(req.url).pathname;
        var query = url.parse(req.url).query
        var params = querystring.parse(query);


        if(query === null){
            fileServer.serve(req, res);
        }else{ 
            if ('p' in params && isJSON(params['p'])) {
                var p = JSON.parse(params['p']);
                switch (Object.keys(p)[0]) {
                    ...
                }
            } else {
                res.write(JSON.stringify({
                    "errorMessage": "invalid request format"
                }));
                res.end();
            }
        }
    }).resume();
});
H.C
  • 565
  • 7
  • 28