Javascript 将路由限制为Express和Nodejs中的静态文件

Javascript 将路由限制为Express和Nodejs中的静态文件,javascript,html,node.js,express,Javascript,Html,Node.js,Express,我目前正试图将路由限制为未登录的用户。我的主要问题是,即使我使用get方法定义页面,例如: app.get('/alpha/information', isLoggedIn, function(req, res){ res.sendFile(path.join(__dirname + '/alpha/pages/Example.html')); }); 用户只需将url编辑为:http://localhost:3000/alpha/pa

我目前正试图将路由限制为未登录的用户。我的主要问题是,即使我使用get方法定义页面,例如:

 app.get('/alpha/information', isLoggedIn,
        function(req, res){
            res.sendFile(path.join(__dirname + '/alpha/pages/Example.html'));
        });
用户只需将url编辑为:
http://localhost:3000/alpha/pages/Example.html
并访问该页面。现在我已经读了几个类似的问题,但我找不到答案。我得到的一些启发是:。尽管如此,我还是无法找到解决问题的办法

我当前的文件结构是:

我试图限制对Example.html、ExampleTwo.html和blabla.html的访问

我正在使用此代码设置请求,但我猜它们可能不正确:

app.use(express.static(path.join(__dirname, 'Alpha')));
app.use(express.static(path.join(__dirname, '/')));
app.use('/', express.static(__dirname + '/login.html'));
这个
app.use('/',express.static(uu dirname+'/login.html')
专门用于将默认的
localhost:3000/
加载为
localhost:3000/login

如何限制对所有静态html文件的访问,而不必为每个静态html文件编写路由

中间件功能:

function isLoggedIn(req, res, next) {
        console.log('here is Authenticated', req.isAuthenticated())
        if (req.isAuthenticated()){
            return next();
        }
        res.redirect('/login');
    }

您正在将完整的Alpha目录设置为公共目录,因此所有内容都可以访问。此技术通常用于服务js/css/图像

您可以使用变量route来获取html文件:

url:localhost:3000/alpha/示例

app.get('/alpha/:name', function(req, res) {
    var page = req.params.name;
    res.sendFile(path.join(__dirname + '/alpha/pages/' + page + '.html'));
})

注意资本化

以下是如何实现资本化的概念:

var express = require('express'),
    path = require('path');
    app = express();

app.use(function(req, res, next) {
    // Use your req.isAuthenticated logic here, that's all
    console.log('I am called before static middleware.');
    return next();
});
app.use(express.static( path.join(__dirname, 'public')));
app.use(function(req, res, next) {
    console.log('I am called after static middleware.');
    return next();
});

app.get('/', showClientRequest, function(req, res) {
    res.send('Hi! I am direct message from server :)');
});

function showClientRequest(req, res, next) {
    console.log('You can do something here too...');
    return next();
}

app.listen(3000);
完整回购:

克隆节点作弊,运行
节点应用程序
,然后运行
npm安装快车


乐于助人

您可以通过附加另一个中间件来限制express静态中间件

var express = require("express");
var path = require( "path" );
var app = express();

function isLoggedIn( req, res, next ) {
   console.log("trying restricted file");
   next();
}

app.use( '/Alpha', isLoggedIn, express.static( path.join( __dirname, 'Alpha' ) ) );
app.use( express.static( path.join( __dirname, 'anonymous' ) ) );

app.listen( 3000 );
通过每次调用
localhost:3000/restricted/*
时执行此操作,将通过isLoggedIn函数实现


编辑:根据您的文件结构修改代码。

您可以在不希望在未登录的情况下访问的任何其他路由(甚至静态文件)之前添加全局身份验证中间件功能。如何识别用户是否登录?如果您知道可以在使用express.static中间件之前先使用中间件。@Zeeshanhassanmon查看文件夹结构,这就是我调整登录系统的方式。我只需要通过路由限制对静态html文件的访问。中间件功能已更新。@coderJoe查看我的答案以及完整的工作和测试节点欺骗,如果对您有效,请将答案标记为正确。Zeeshan Hassan提供的答案不是*针对特定问题定制的。