我希望从我网站上的任何请求(图像,字体,CSS,js等)中捕获所有数据,以便我可以捕获文件详细信息,特别是文件名和文件大小。我发现了几乎相同的问题/解决方案:
Node.js : How to do something on all HTTP requests in Express?
但Express v4似乎不推荐使用该解决方案。有一个简单的解决方案吗?作为另一种方法,我尝试了以下解决方案,没有运气:
var express = require("express");
var path = require("path");
var port = process.env.PORT || 3000;
var app = express();
var publicPath = path.resolve(__dirname, "public");
app.use(express.static(publicPath));
app.get("/", function(req, res){
// I want to listen to all requests coming from index.html
res.send("index.html");
});
app.all("*", function(){
// can't get requests
})
app.listen(port, function(){
console.log(`server listening on port ${port}`);
});
此外,我不希望Fiddler / Charles这样做,因为我希望在我的网站上显示这些数据。
答案 0 :(得分:1)
快递路线以订单为基础。请注意,您在问题中链接的答案已定义中间件,并在所有其他路径之前使用。
其次,您尝试实施需要中间件的东西,而不是通配符路由。您在问题中提供的链接模式不会根据其docs弃用。
app.use(function (req, res, next) {
// do something with the request
req.foo = 'testing'
next(); // MUST call this or the routes will not be hit
});
app.get('/', function(req, res){
if (req.foo === 'testing') {
console.log('works');
}
res.send("index.html");
});