router.post("/login", function(req, res, next) { var file = "c:\a.txt"; var str = JSON.stringify(req); fs.appendFile(file, str, function(err){ if(err) { console.log(err); } else { console.log("写入文件ok"); } }); });
Initially learning nodejs, when a request comes, I want to see how many things are in this request. I can directly use the console to print it out, but the console is too useless, so I just want to save it to notepad. When I open it with a local IDE, I get an error in JSON.stringify(req).
Here I put var str = req; which doesn’t work. If I replace it with this one, what is saved in txt is [object Object].
Please help me, God, what is the problem?
req cannot be serialized into json. If you want to see what’s in it except the console, you can only use debug
req contains circular reference fields, so it cannot be stringify. Give an example
If you want to view req, you can view it through debugging
Command line debugging
node debug
chrome debugging
node --inspect
Wanting to see req in a file is easy.
You don’t need to write the file yourself at all. Just enter it directly on the command line
node app.js > ./a.log
, and all the contents of req will be written to the a.log file in the current working directory. Be careful to replace app.js with the one you want to run. js file