0

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);
Roman
  • 3,563
  • 5
  • 48
  • 104
  • Note: the code `new(nodeStatic.Server)()` is incorrect. It should be `new nodeStatic.Server()` or `new nodeStatic.Server(path)`. – jarmod May 26 '20 at 15:00

1 Answers1

0

The documents say:

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

For example:

fileServer.serveFile('xyz.html', 200, {}, request, response);
jarmod
  • 71,565
  • 16
  • 115
  • 122
  • This does not work. I assume because there is already an index.html. I need to overwrite it OR edit it OR server a different index.html. – Roman May 26 '20 at 14:25
  • You're asking how to serve a different file. Why will this not work? It should allow you to serve *any* file that you want to serve. – jarmod May 26 '20 at 14:36
  • I get an error, Bad Gateway 502. Its probably smarter to use a templating engine, so I can edit the index.html at the necessary points. I need this because I want to load the main.js and main.css from a different source if I am in the office and have the office IP – Roman May 26 '20 at 14:56
  • 1
    You should debug the problem. At a guess you've indicated the filename or your serving path incorrectly. Create a very simple html file, put it in your serving path, and then serve it in response to a specific request so that you can test that you have the basics working correctly. – jarmod May 26 '20 at 15:04