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

后端 未结 3 1987
耶瑟儿~
耶瑟儿~ 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条回答
  •  一生所求
    2021-01-12 21:20

    You need body-parser that will create body object for you in your request.
    To do that npm install body-parser

    var bodyParser = require('body-parser')//add this
    
    app.use(bodyParser())//add this before any route or before using req.body
    
    app.use((req, res) => {
      console.log(req.body); // this is what you want           
    
      res.on("finish", () => {
    
        console.log(res);
    
      });
    
    });
    

提交回复
热议问题