Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 获取morgan组合格式字符串_Node.js_Morgan - Fatal编程技术网

Node.js 获取morgan组合格式字符串

Node.js 获取morgan组合格式字符串,node.js,morgan,Node.js,Morgan,如何返回morgan组合格式字符串 async function sample(){ const res = await axios.get("http://localhost:3000/sample") const morganFormattedString = morgan('combined', {stream: {write: res => res }}) } res将是一个对象,我需要将res的对象转换为下面的字符串,该字符串由morgan内部完成 ::ffff:12

如何返回morgan组合格式字符串

async function sample(){
  const res = await axios.get("http://localhost:3000/sample")
  const morganFormattedString = morgan('combined', {stream: {write: res => res }}) 
}
res将是一个对象,我需要将res的对象转换为下面的字符串,该字符串由morgan内部完成

::ffff:127.0.0.1---[18/Jan/2019:04:59:10+0000]“GET/sample HTTP/1.1“200 2”“”Mozilla/5.0(linux)AppleWebKit/537.36(KHTML,如Gecko)jsdom/11.12.0”

返回用于记录传入请求的服务器端中间件。您试图使用它在客户端记录传出的请求,这不是它的目的。因为您试图完成的工作应该是可行的,但是为了让它以
组合的
格式记录请求,您必须这样做

作为参考:

morgan.format('combined',':remote addr-:remote user[:date[clf]]“:method:url HTTP/:HTTP version”:status:res[content length]“:referer”“:user agent”“)

谢谢您的回复,但如果我保存代码,nodemon将重新启动index.js并创建文件夹,我可以以正确的格式记录morgan日志,这就是为什么我编写了一个自定义函数来检查,因为morgan返回的是字符串格式,winston返回的是开发者提供的对象格式。检查这个问题我已经详细解释了。这个问题是问题中的第二个问题,你应该提到另一个问题以及你在这个问题中面临的问题的完整描述。你能检查另一个问题吗,这样我就可以删除这个问题。我把所有的东西都贴在那里了