简体   繁体   English

显示Express中每个请求的日志时间?

[英]show log time every request in Express?

I've some problem in here. 我在这里有些问题。 I don't know why this is happened. 我不知道为什么会这样。 I put timestamps code with Moment.js library, not only with that library, recently i created manually for showing timestamps but when i send request, time not updated. 我将时间戳记代码添加到Moment.js库中,不仅是该库,最近我是手动创建以显示时间戳记的,但是当我发送请求时,时间未更新。 I put datetime in my file route. 我在文件路径中添加了datetime。 But this is work in server file. 但这在服务器文件中有效。

So for example 所以举个例子

server.js server.js

var express = require('express')
var app = express()
var moment = require('moment')

app.use(function(req, res, next){
  console.log(moment().format('HH:mm:ss') + ' From server.js') //Showing time now
  next();
  })

app.use('/', index)
app.listen(8001)

routes/index.js 路线/ index.js

var express = require('express')
var app = express()
var router = express.Router()
var moment = require('moment')
var timestamps = moment().format('HH:mm:ss')

router.get('/', function(req, res){
  console.log(timestamps + ' From routes/index.js')
})

module.exports = routes

And i begin test for my code for first time GET localhost:8001/ 并且我开始第一次测试我的代码GET localhost:8001 /

My system time showing 16:20:51 我的系统时间显示16:20:51

[nodemon] 1.11.0
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node ./bin/www`
16:20:51 From Server.js
16:20:51 From routes/index.js

And the second request my system time showing 16:22:52 第二个请求我的系统时间显示16:22:52

[nodemon] 1.11.0
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node ./bin/www`
16:22:52 From Server.js
16:20:51 From routes/index.js

Nah, the second request still get existing time log from first request, this is only happened in routes. 不,第二个请求仍然从第一个请求获取现有的时间日志,这仅发生在路线中。 But work with well in server.js 但是在server.js中可以很好地工作

Is it happened because variable timestamps outside function? 是否因为函数外部的可变时间戳而发生? But when i running without variable, it's worked. 但是,当我没有变量运行时,它就起作用了。

router.get('/', function(req, res){
  console.log(moment().format('HH:mm:ss') + ' From routes/index.js')
})

Why this is can be happened? 为什么会这样呢? Thanks 谢谢

You cache the timestamp value. 您缓存时间戳记值。

Modify your route file this way: 通过以下方式修改您的路由文件:

var express = require('express')
var app = express()
var router = express.Router()
var moment = require('moment')

router.get('/', function(req, res){
  var timestamps = moment().format('HH:mm:ss')
  console.log(timestamps + ' From routes/index.js')
})

module.exports = routes

You have to understand that file is included once the application started and until it is running. 您必须了解该文件在应用程序启动后一直包含在内,直到运行为止。 The only part is called from time to time here is the function handler of the route: 不时调用的唯一部分是路由的函数处理程序:

function(req, res){
  var timestamps = moment().format('HH:mm:ss')
  console.log(timestamps + ' From routes/index.js')
}

You were on right track! 你走对了! It is because you define timestamps outside the middleware*. 这是因为您在中间件*之外定义了timestamps The middleware is the function you register to the route and it gets called on each request that matches the specified url. 中间件是您注册到路由的函数,并且在与指定url匹配的每个请求上都会调用该中间件。 So when you invoke moment() inside the middleware it returns the actual date and time of the request, since this function is executed only during the procession of a request. 因此,当您在中间件中调用moment() ,它会返回请求的实际日期和时间,因为此函数仅在请求处理期间执行。 While in your case the code with the timestamps defined outside the function is executed only once - at the startup of the application when you require routes/index.js . 在您的情况下,在函数外部定义的带有timestamps的代码仅执行一次-在需要routes/index.js的应用程序启动时执行。 But you may still use variable, just define it on the right place: 但是您仍然可以使用变量,只需在正确的位置进行定义即可:

var express = require('express')
var app = express()
var router = express.Router()
var moment = require('moment')
var format = 'HH:mm:ss';

router.get('/', function(req, res){
  var timestamps = moment().format(format)
  console.log(timestamps + ' From routes/index.js')
})

module.exports = routes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM