Node.js-如何隐藏html页面?

问题描述 投票:0回答:1

我有未登录的用户不应该看到的html页面。我使用下面的命令,并且html页面公开了。

app.use(express.static('public'));

例如,我不想让未登录的用户看到此页面。

http://localhost:3000/admin.html

注意:我要说的不是cookie。当您在工具栏中输入html页面的地址时,如果未登录,则应该无法访问该页面。

javascript html node.js express public
1个回答
0
投票

创建定制的static中间件,使用该中间件您可以验证路径(此情况下的文件名)。

我将尝试用示例代码中的注释进行解释:

// path.join here makes it work cross platform with Windows / Linux / etc
var statics = express.static(path.join(__dirname, 'public'));


function secureStatic(pathsToSecure = []) {
  return function (req, res, next) {
    if (pathsToSecure.length === 0) {
      return statics(req, res, next); // Do not secure, forward to static route
    }

    if (pathsToSecure.indexOf(req.path) > -1) {
      return res.status(403).send('<h1>403 Forbidden</h1>'); // Stop request
    }

    return statics(req, res, next); // forward to static route
  };
}


// add public files. List all "private" paths (file)
app.use(secureStatic(['admin.html'])); // instead of app.use(express.static('public'));

但是,使用这种中间件,没有人可以通过您的快速服务器请求admin.html文件。

© www.soinside.com 2019 - 2024. All rights reserved.