Javascript Node.js-如何隐藏html页面?
我有html页面,没有登录的用户不应该看到这些页面。我使用了下面的命令,我的html页面公开了Javascript Node.js-如何隐藏html页面?,javascript,html,node.js,express,public,Javascript,Html,Node.js,Express,Public,我有html页面,没有登录的用户不应该看到这些页面。我使用了下面的命令,我的html页面公开了 app.use(express.static('public')); 例如,我不希望未登录的用户看到此页面 注意:我说的不是饼干。当您在工具栏中输入html页面的地址时,如果该页面未登录,它将无法访问该页面。创建一个自定义静态中间件,使用该中间件您可以验证路径(本例中为文件名) 我将尝试在示例代码中用注释进行解释: // path.join here makes it work cross pla
app.use(express.static('public'));
例如,我不希望未登录的用户看到此页面
注意:我说的不是饼干。当您在工具栏中输入html页面的地址时,如果该页面未登录,它将无法访问该页面。创建一个自定义
静态
中间件,使用该中间件您可以验证路径(本例中为文件名)
我将尝试在示例代码中用注释进行解释:
// 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'));
//path.join使其能够与Windows/Linux/etc跨平台工作
var statics=express.static(path.join(uu dirname,'public');
函数secureStatic(路径安全=[]){
返回函数(req、res、next){
if(pathsToSecure.length==0){
返回静态(req、res、next);//不安全,转发到静态路由
}
if(pathsToSecure.indexOf(请求路径)>-1){
返回res.status(403.send('403禁止');//停止请求
}
返回静态(req、res、next);//转发到静态路由
};
}
//添加公共文件。列出所有“专用”路径(文件)
app.use(secureStatic(['admin.html']);//而不是app.use(express.static('public');
但是,有了这个中间件,没有人可以通过express server请求
admin.html
文件。您需要某种条件,比如app.get('/protected',(req,res)=>{if(req.user){…
但是如果用户手工编写,难道他还看不到吗?例如www.example.com/protected.html。因为它不是/protected,而是/protected.htmlIt听起来像是我们在这里讨论授权。你可以通过在apache auth、nginx后面运行节点,或者使用passport.I thin之类的快速中间件来实现k问题不在于授权。因为/user和/user.html不相同。用户可以在工具栏中键入html页面的名称,然后再次访问。因为服务器控制的是/user,而不是/user.html。