Heim > Fragen und Antworten > Hauptteil
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");
}
});
});
Ich lerne zunächst NodeJS. Wenn eine Anfrage eingeht, möchte ich sehen, wie viele Dinge in der Anfrage enthalten sind, aber die Konsole ist zu nutzlos, also möchte ich sie einfach speichern Notepad zur Verwendung. Öffnen Sie die lokale IDE und sehen Sie nach. In JSON.stringify(req) wird ein Fehler gemeldet.
Mein var str = req; funktioniert hier nicht. Wenn ich es durch dieses ersetze, wird in txt [object Object] gespeichert.
Bitte hilf mir, Gott, was ist das Problem dabei?
高洛峰2017-05-16 13:22:56
let a = {}
let b = {a}
a.b = b
JSON.stringify(a) //TypeError: Converting circular structure to JSON
a.toString() //[Object Object]
router.post("/login", function(req, res, next) {
var file = "c:\\a.txt";
var str = JSON.stringify(req);
debugger; //断点
res.end('')
});
命令行调试node debug <main.js>
chrome调试node --inspect <main.js>
滿天的星座2017-05-16 13:22:56
想要在文件中看 req 很简单。
router.post("/login", function(req, res, next) {
console.log(req);
});
压根儿就不需要自己进行文件写入的操作,直接命令行输入 node app.js > ./a.log
, req的所有内容就会写入到当前工作目录的 a.log 这个文件中,注意把 app.js 换成你要运行的js文件