我想让它使得每当用户访问特定的路线时,写/附加到一个文件。我想把代码放在路由“myroute”中。我担心的是,如果多人访问我的快速应用程序,可能会有一些文件访问/写入问题。如何在用户访问文件时将快速路由记录到文件?
我希望下面的示例输出文件中:
Accessed <current date>
Accessed <current date>
Accessed <current date>
Accessed <current date>
....
我想让它使得每当用户访问特定的路线时,写/附加到一个文件。我想把代码放在路由“myroute”中。我担心的是,如果多人访问我的快速应用程序,可能会有一些文件访问/写入问题。如何在用户访问文件时将快速路由记录到文件?
我希望下面的示例输出文件中:
Accessed <current date>
Accessed <current date>
Accessed <current date>
Accessed <current date>
....
如果你想每次运行某些代码某条路,我建议为它创建中间件。
http://expressjs.com/guide/using-middleware.html
var app = express();
app.use('/specific/route', function(req, res, next) {
//add your code to run every time route is hit
next();
}
app.get('/specific/route', function (req, res, next) {
res.send('hello world');
}
你可以简单地这样做:
var app = express();
var logger = function(request, response, next) {
var accessedPath = request.path;
// do something with the accessed path
next();
}
app.use(logger); // for all routes.
app.use('/admin', logger); // for specified routes
在你想要的输出,你可以添加其他列指定访问指定路线的特定用户。
文件访问/写入问题
没有任何调查或没有太多的想法。如果您的访问日志必须是文件,则可以使用一些排队系统。 你记录将是这样的:
var logger = function(request, response, next) {
var accessedPath = request.path;
// get the user info
// get datetime/timestamp/date/time Date.now()
// add userinfo, accessed path and timestamp to the queue.
next();
}