Node.js:如何处理 Express 中的所有 HTTP 请求?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7263626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Node.js : How to do something on all HTTP requests in Express?
提问by Adam
So I would like to do something like:
所以我想做一些类似的事情:
app.On_All_Incoming_Request(function(req, res){
console.log('request received from a client.');
});
the current app.all()requires a path, and if I give for example this /then it only works when I'm on the homepage, so it's not really all..
当前app.all()需要一个路径,如果我举个例子,/那么它只在我在主页上时才有效,所以它并不是全部..
In plain node.js it is as simple as writing anything after we create the http server, and before we do the page routing.
在普通的 node.js 中,它就像在我们创建 http 服务器之后、在我们进行页面路由之前编写任何东西一样简单。
So how to do this with express, and what is the best way to do it?
那么如何用 express 做到这一点,最好的方法是什么?
回答by Rahman Kalfane
Express is based on the Connectmiddleware.
Express 基于Connect中间件。
The routing capabilities of Express are provided by the routerof your app and you are free to add your own middlewares to your application.
Express 的路由功能由router您的应用程序提供,您可以自由地将自己的中间件添加到您的应用程序中。
var app = express.createServer();
// Your own super cool function
var logger = function(req, res, next) {
console.log("GOT REQUEST !");
next(); // Passing the request to the next handler in the stack.
}
app.configure(function(){
app.use(logger); // Here you add your logger to the stack.
app.use(app.router); // The Express routes handler.
});
app.get('/', function(req, res){
res.send('Hello World');
});
app.listen(3000);
It's that simple.
就这么简单。
(PS : If you just want some logging you might consider using the loggerprovided by Connect)
(PS:如果您只是想要一些日志记录,您可以考虑使用Connect 提供的记录器)
回答by Abadis
You should do this:
你应该做这个:
app.all("*", function (req, resp, next) {
console.log(req); // do anything you want here
next();
});
回答by Tarun Rawat
You can achieve it by introducing a middleware function. app.use(your_function) can be of help. app.use with accept a function that will get executed on every request logged to your server. Example:
你可以通过引入一个中间件功能来实现。app.use(your_function) 可以提供帮助。app.use 与接受一个函数,该函数将在记录到您的服务器的每个请求上执行。例子:
app.use((req,res,next)=>{
console.log('req recieved from client');
next();//this will invoke next middleware function
})
回答by vkarpov15
Express supports wildcards in route paths. So app.all('*', function(req, res) {})is one way to go.
Express 支持路由路径中的通配符。所以app.all('*', function(req, res) {})是一种方法。
But that's just for route handlers. The difference is that an Express route handler is expected to send a response, and, if it doesn't, Express will never send a response. If you want to do something without explicitly sending a response, like check for a header, you should use Express middleware. app.use(function(req, res, next) { doStuff(); next(); }
但这仅适用于路由处理程序。不同之处在于 Express 路由处理程序应该发送响应,如果没有,Express 将永远不会发送响应。如果您想在不显式发送响应的情况下执行某些操作,例如检查标头,则应使用Express 中间件。app.use(function(req, res, next) { doStuff(); next(); }

