首页 > 解决方案 > 当我在 apt.get() 中添加 console.print() 语句来调试我的 javascript 代码时,Nodejs 服务器吓坏了

问题描述

我试图在 NodeJs 服务器上的 app.get() 期间从 IBM Watson 获取信息。从 Watson 获取信息的代码有效,我正在努力将其发送回客户端。但是,每当我添加一个额外的 console.print(words) 时,它就会吓坏并抱怨在我添加新的console.print()语句之前完全不同的行。

app.get("/text/emotion", (req,res) => {
    const nlu = getNLUInstance();
    //console.log( "request /text/emotion");
    var parameters = { text: "I am having a bad day today, \
        could someone help me please",features : {emotion: {}}}
    ret = nlu.analyze( parameters ) //, function( error, response){
           .then (analysisResults => {
            console.log(JSON.stringify(analysisResults, null, 2));
            console.log( "in here")
           })
           .catch(err=> {console.log( "error",err);
           });
          
  
    console.log( ret );
    return res.send({"happy":"10","sad":"50"});
});

app.get("/text/sentiment", (req,res) => {
    return res.send("text sentiment for "+req.query.text);
});

let server = ***app.listen(8080, ()*** => {
    console.log('Listening', server.address().port);
});

这可行,但是如果我在 console.log(ret) 上方添加另一个 console.log("Here") 语句,它就会对 app.listen(8080...调用感到恐惧。错误指向应用程序之间的点 '.' 。听

所以代码不起作用

    console.log( "here"); 
    console.log( ret );
    return res.send({"happy":"10","sad":"50"});
});

但是这段代码确实

    console.log( ret );
    return res.send({"happy":"10","sad":"50"});
});

这是添加额外的 console.log() 时的错误输出

[nodemon] starting `node sentimentAnalyzerServer.js`
events.js:292
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE: address already in use :::8080
    at Server.setupListenHandle [as _listen2] (net.js:1313:16)
    at listenInCluster (net.js:1361:12)
    at Server.listen (net.js:1447:7)
    at Function.listen (/home/project/cazgi-IBM-Watson-NLU-Project-1/sentimentAnalyzeServer/node_modules/express/lib/application.js:618:24)
    at Object.<anonymous> (/home/project/cazgi-IBM-Watson-NLU-Project-
1/sentimentAnalyzeServer**/sentimentAnalyzerServer.js:64:18)**
    at Module._compile (internal/modules/cjs/loader.js:1137:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
    at Module.load (internal/modules/cjs/loader.js:985:32)
    at Function.Module._load (internal/modules/cjs/loader.js:878:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
Emitted 'error' event on Server instance at:
    at emitErrorNT (net.js:1340:8)
    at processTicksAndRejections (internal/process/task_queues.js:84:21) {
  code: 'EADDRINUSE',
  errno: 'EADDRINUSE',
  syscall: 'listen',
  address: '::',
  port: 8080
}
[nodemon] app crashed - waiting for file changes before starting...

似乎无论我是否放置了额外的 console.log 语句,它只是不喜欢它们。或者有没有更好的方法来调试这些东西。我正在使用终端来完成所有这些工作。我对 Node.js 和 javascript 很陌生,但这对我来说似乎不合适。

标签: javascriptnode.jsconsole.log

解决方案


推荐阅读