Node.js + Express - How to log the request body and response body

后端 未结 3 2011
耶瑟儿~
耶瑟儿~ 2021-01-12 20:51

I have a small api I have built using Node.js and express. I am trying to create a logger and I need log the request body AND response body.

app.use((req, re         


        
3条回答
  •  梦毁少年i
    2021-01-12 21:26

    install npm install body-parser

    and use this snippet,

    var express = require('express')
    var bodyParser = require('body-parser')
     
    var app = express()
     
    // create application/json parser
    var jsonParser = bodyParser.json()
     
    to get json response
    app.use(jsonParser, function (req, res) {
      console.log(req.body); // or console.log(res.body);
    })
    

提交回复
热议问题