morgan(
":remote-addr :method :url :status :res[content-length] :response-time ms"
);例如,如果:res[content-length]不可用,那么它将以"-“代替。我怎么才能把这个冲刺变成别的东西呢?
发布于 2022-10-27 01:20:01
"-"是在compile函数这里中硬编码的,当令牌后面的值是undefined时运行。
当令牌后面的值(即方法 )丢失时,可以使用该undefined提供自定义字符串。您可以使用它来覆盖传递给morgan的格式字符串中使用的预定义令牌。下面是一个示例,覆盖预定义的:res令牌,如果令牌值为undefined,则用<!>替换<!>。
const express = require('express')
const morgan = require('morgan')
const app = express()
const origRes = morgan.res
morgan.token('res', function customGetResponseHeader(req, res, field) {
const resp = origRes(req, res, field)
if (resp === undefined) {
// Custom string here
return '<!>'
}
})
app.use(morgan(":remote-addr :method :url :status :res[content-length] :response-time ms"))
app.get('/', function (req, res) {
res.send('hello, world!')
})
app.listen(3000, 'localhost', () => {
console.log('listening')
})https://stackoverflow.com/questions/74211201
复制相似问题