簡體   English   中英

如何在node.js中記錄請求對象的所有屬性?

[英]how to log all properties of the reqest object in node.js?

我是接觸node.js的新手,並且對在任何請求到達服務器時req參數將具有哪種數據感到好奇。 我已經嘗試過For循環,但似乎沒有記錄任何內容。

var http = require('http');

var server = http.createServer(function( req, res){
  for (var key in req) {
    if (req.hasOwnProperty(key)) {
        console.log(key + " -> " + req[key]);
    }
}

      res.end("Hi there \n");
});

server.listen(3000, function(){
    console.log('Server on 3000');
});

您可以將對象本身記錄到控制台,節點將為您處理。

var http = require('http');

var server = http.createServer(function(req, res){
  console.log(req)
  res.end("Hi there \n");
});

server.listen(3000, function(){
    console.log('Server on 3000');
});

讓您:

$ node script.js 
Server on 3000
IncomingMessage {
  _readableState: 
   ReadableState {
     objectMode: false,
     highWaterMark: 16384,
     buffer: BufferList { head: null, tail: null, length: 0 },
     length: 0,
     pipes: null,
     pipesCount: 0,
     flowing: null,
     ended: false,
     endEmitted: false,
     ...

我認為這段代碼可以幫助您:

console.log(JSON.stringify(req, null, 4));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM