Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js console.log() not logging anything

Tags:

node.js

Trying out node.js for the first time. Set up node, set up the example app from the nodejs.org site. 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?

(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/'); 
like image 213
chrismanderson Avatar asked Dec 03 '11 01:12

chrismanderson


People also ask

Does console log work in node?

log() function from console class of Node. js is used to display the messages on the console. It prints to stdout with newline. Parameter: This function contains multiple parameters which are to be printed.

How do I enable logging in node js?

Logging in Node. By default, it will not produce any output. To enable this logger, you have run your application with a special environment variable, called DEBUG . Once you do that, the debug module will come to life and will start producing log events for stdout.

What does console log () do?

The console. log() is a function in JavaScript that is used to print any kind of variables defined before in it or to just print any message that needs to be displayed to the user.

Why you should not use console log?

Untrustworthy Information Most of the time, calling console. log() when the console is not yet active only results in a reference to the object being queued, not the output the console will contain. As a workaround, you'll need to either clone the information or serialize snapshots of it.


1 Answers

In a node.js server console.log outputs to the terminal window, not to the browser's console window.

How are you running your server? You should see the output directly after you start it.

like image 127
david Avatar answered Oct 10 '22 09:10

david