suchen

Heim  >  Fragen und Antworten  >  Hauptteil

node.js – Speichern Sie die Hot-Anfrage in nodejs und verwenden Sie JSON.stringify(req), um einen Fehler zu melden.

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?

世界只因有你世界只因有你2829 Tage vor923

Antworte allen(3)Ich werde antworten

  • 为情所困

    为情所困2017-05-16 13:22:56

    req是无法json序列化的,想看里面有什么除了console就只能是debug

    Antwort
    0
  • 高洛峰

    高洛峰2017-05-16 13:22:56

    req中存在循环引用的字段,所以无法stringify。举个例子

    let a = {}
    let b = {a}
    a.b = b
    JSON.stringify(a) //TypeError: Converting circular structure to JSON
    a.toString() //[Object Object]

    如果你想查看req,可以通过调试来查看

    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>

    Antwort
    0
  • 滿天的星座

    滿天的星座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文件

    Antwort
    0
  • StornierenAntwort