1
votes

how can i return the morgan combined format string

async function sample(){
  const res = await axios.get("http://localhost:3000/sample")
  const morganFormattedString = morgan('combined', {stream: {write: res => res }}) 
}

the res will be an object , i need to get the object of res to be converted as below string which is done by the morgan internally

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

1

1 Answers

1
votes

morgan returns server-side middleware which is supposed to be used for logging incoming requests. You're trying to use it client-side to log outgoing requests, which isn't its purpose. For what you're trying to accomplish axios-debug-log should work, but in order for it to log requests in the combined format, you'll have to configure it yourself.

For reference, this is how morgan currently defines the combined log format:

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