我在Express服务器上像这样使用morgan:
const morgan = require('morgan');
app.use(morgan('dev'));在我的日志中,我通常会看到这样的内容:
GET /?prompt_ids={%22foo%22:%22bar%22} 200 122.495 ms - -我的问题是-有没有一种方法可以使用morgan记录查询字符串,其中的字符不会转义?
它看起来像这样:
GET /?prompt_ids={"foo":"bar"} 200 122.495 ms - -发布于 2018-02-28 08:01:00
基本上,您希望将JavaScript函数decodeURI()应用于摩根记录的url。
你可以定义一个自定义的日志布局,就像“dev”布局一样,只需要做一个小小的改动。
为了简单起见,我们可以直接使用“dev”布局的细节from the docs。
因此,不要使用app.use(morgan('dev')),只需使用:
morgan(function (tokens, req, res) {
return [
tokens.method(req, res),
decodeURI(tokens.url(req, res)), // I changed this from the doc example, which is the 'dev' config.
tokens.status(req, res),
tokens.res(req, res, 'content-length'), '-',
tokens['response-time'](req, res), 'ms'
].join(' ')
})编辑:如果这不能很好地工作,你可以使用decodeURIComponent()而不是decodeURI(),根据这个问题:NodeJS Express encodes the URL - how to decode
https://stackoverflow.com/questions/49019844
复制相似问题