Node static serve different index.html for a certain IP address

176 views Asked by At

I am using node static for a really simple web app.

I need to server a different index.html for a certain client IP.

This is my code atm.:

var fileServer = new(nodeStatic.Server)();
var app = http.createServer(options, function(req, res) { 
    // Office IP so I dont need code
    var staticIp = "111.11.11.11"; 
    var securityCode = "SECRETKEY";
    if (staticIp == req.headers['x-real-ip']) {
        // TO DO: Server an other index.html
    } else if (req.url.includes(securityCode)) {
        fileServer.serve(req, res);
    } else {
        res.writeHead(200, {"Content-Type": "text/plain"});
        res.write("Access Denied");
        res.end();
    }
}).listen(8443);
1

There are 1 answers

4
jarmod On

The documents say:

If you want to serve a specific file, use the serveFile method:

For example:

fileServer.serveFile('xyz.html', 200, {}, request, response);