Node.js Nodejs express捕获对服务器上公共文件的调用
我正在node.js express和Angularjs中构建一个站点。 整个网站是静态的,我通过公共服务提供,如下所示:Node.js Nodejs express捕获对服务器上公共文件的调用,node.js,express,Node.js,Express,我正在node.js express和Angularjs中构建一个站点。 整个网站是静态的,我通过公共服务提供,如下所示: app.use(express.static(path.join(__dirname, 'public'),{})); 如何在express中捕获对特定页面的呼叫 我试过这个: app.all("/app",multipart,function(req, res, next){ console.log("Checking if the user is logged
app.use(express.static(path.join(__dirname, 'public'),{}));
如何在express中捕获对特定页面的呼叫
我试过这个:
app.all("/app",multipart,function(req, res, next){
console.log("Checking if the user is logged");
});
/app
是一个静态html页面,位于public
目录中,但从未到达console.log上的我的断点。
我试图阻止对静态目录中某些文件的访问。也许还有其他更好的办法。我开门
提前感谢你的帮助 我认为您可以简单地通过将所需路由放置在中间件
express.static
之上来对它们进行优先级排序。例如:
app.all("/app",multipart,function(req, res, next){
console.log("Checking if the user is logged");
next(); // or end the request ?
});
app.use(express.static(path.join(__dirname, 'public'),{}));
我认为您可以简单地通过将所需路由放置在中间件
express.static
之上来对它们进行优先级排序。例如:
app.all("/app",multipart,function(req, res, next){
console.log("Checking if the user is logged");
next(); // or end the request ?
});
app.use(express.static(path.join(__dirname, 'public'),{}));
我创建了一个示例来展示如何管理这个场景 项目的目录树,其中公共目录包含公共资产,其中有一个受保护目录,用于存储受保护的集合
├── app.js
└── public
├── index.html
└── protected
└── app.html
在app.js
文件中,我正在使用一个中间件app.use('/protected/*',..)
,它将在点击app.use(express.static(..)之前执行代码>在此中间件中,我们将检查用户是否允许使用受保护的资产,如果允许,我们将调用下一个函数,让express继续执行后续的中间件,否则它将向用户发送一个响应,说不允许他使用资产
var express = require('express');
var app = express();
var path = require('path');
function isUserAllowed(fn) {
fn(null, false);
}
app.use('/protected/*', function(req, res, next) {
isUserAllowed(function(err, allowed) {
if (!allowed) {
res.status(401).send('You are not allowed to see this page.');
} else {
next();
}
});
});
app.use(express.static(path.join(__dirname, 'public')));
app.listen(4000, function() {
console.log('server up and running');
});
我创建了一个示例来展示如何管理这个场景
项目的目录树,其中公共目录包含公共资产,其中有一个受保护目录,用于存储受保护的集合
├── app.js
└── public
├── index.html
└── protected
└── app.html
在app.js
文件中,我正在使用一个中间件app.use('/protected/*',..)
,它将在点击app.use(express.static(..)之前执行代码>在此中间件中,我们将检查用户是否允许使用受保护的资产,如果允许,我们将调用下一个函数,让express继续执行后续的中间件,否则它将向用户发送一个响应,说不允许他使用资产
var express = require('express');
var app = express();
var path = require('path');
function isUserAllowed(fn) {
fn(null, false);
}
app.use('/protected/*', function(req, res, next) {
isUserAllowed(function(err, allowed) {
if (!allowed) {
res.status(401).send('You are not allowed to see this page.');
} else {
next();
}
});
});
app.use(express.static(path.join(__dirname, 'public')));
app.listen(4000, function() {
console.log('server up and running');
});