Node.js console.log() 不记录任何内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8364790/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Node.js console.log() not logging anything
提问by chrismanderson
Trying out node.js for the first time. Set up node, set up the example app from the nodejs.orgsite. Can start the server fine, but console.log()isn't actually logging anything. Tried the Javascript console in Chrome, Firefox, and Safari - nothing appears in the log. Also checked Console on my Mac just for kicks, nothing was there either. What am I missing?
第一次尝试 node.js。设置节点,从nodejs.org站点设置示例应用程序。可以正常启动服务器,但console.log()实际上没有记录任何内容。在 Chrome、Firefox 和 Safari 中尝试了 Javascript 控制台 - 日志中没有出现任何内容。还检查了我的 Mac 上的控制台只是为了好玩,那里也没有。我错过了什么?
(Here's the example code that works but doesn't log anything.)
(这是有效但不记录任何内容的示例代码。)
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, "127.0.0.1");
console.log('Server running at http://127.0.0.1:1337/');
回答by david
In a node.js server console.logoutputs to the terminal window, not to the browser's console window.
在 node.js 服务器console.log输出到终端窗口,而不是浏览器的控制台窗口。
How are you running your server? You should see the output directly after you start it.
你如何运行你的服务器?您应该在启动后直接看到输出。
回答by atomless
This can be confusing for anyone using nodejs for the first time. It is actually possible to pipe your node console output to the browser console. Take a look at connect-browser-logger on github
对于第一次使用 nodejs 的人来说,这可能会让人感到困惑。实际上可以将您的节点控制台输出通过管道传输到浏览器控制台。看看github上的connect-browser-logger
UPDATE: As pointed out by Yan, connect-browser-logger appears to be defunct. I would recommend NodeMonkey as detailed here : Output to Chrome console from Node.js
更新:正如 Yan 所指出的,connect-browser-logger 似乎已不复存在。我会推荐 NodeMonkey,详情如下:从 Node.js 输出到 Chrome 控制台
回答by J Decker
Using modern --inspect with node the console.log is captured and relayed to the browser.
使用现代的 --inspect 节点,console.log 被捕获并转发到浏览器。
node --inspect myApp.js
节点 --inspect myApp.js
or to capture early logging --inspect-brk can be used to stop the program on the first line of the first module...
或捕获早期日志记录 --inspect-brk 可用于在第一个模块的第一行停止程序...
node --inspect-brk myApp.js
节点 --inspect-brk myApp.js

