【问题标题】:Node static serve different index.html for a certain IP address节点静态为某个 IP 地址提供不同的 index.html
【发布时间】:2020-05-26 14:01:34
【问题描述】:

我正在为一个非常简单的网络应用程序使用 node static。

我需要为某个客户端 IP 提供不同的 index.html。

这是我的密码。:

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);

【问题讨论】:

  • 注意:代码new(nodeStatic.Server)()不正确。应该是new nodeStatic.Server()new nodeStatic.Server(path)

标签: node.js node-static


【解决方案1】:

documents 说:

如果要提供特定文件,请使用 serveFile 方法:

例如:

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

【讨论】:

  • 这不起作用。我假设是因为已经有一个 index.html。我需要覆盖它或编辑它或服务器一个不同的 index.html。
  • 您在询问如何提供不同的文件。为什么这不起作用?它应该允许您提供您想要提供的任何文件。
  • 我得到一个错误,Bad Gateway 502。使用模板引擎可能更聪明,所以我可以在必要的地方编辑 index.html。我需要这个,因为如果我在办公室并拥有办公室 IP,我想从不同的来源加载 main.js 和 main.css
  • 您应该调试问题。猜测您错误地指明了文件名或服务路径。创建一个非常简单的 html 文件,将其放在您的服务路径中,然后根据特定请求提供它,以便您可以测试您的基础是否正常工作。
猜你喜欢
  • 2018-10-21
  • 2021-07-07
  • 1970-01-01
  • 2020-04-26
  • 2021-10-19
  • 2017-04-11
  • 1970-01-01
  • 2016-01-18
  • 2018-06-04
相关资源
最近更新 更多