node.jsnode-static

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


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.


Solution

  • 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();
    });