Node.js Console

      No Comments on Node.js Console




How to Write to console using NodeJs???

Using console object, text can be written on the console.

Writing to console can be done in three ways :

1.) console.log(“Write text here”);

This is used whne you want to print some info on the console.

2.) console.warn(“Warning. customer does not exist”);

This is used when you want something to pay attention to as it might lead to issues but it does not stop the application from functioning.

3.) console.error(“Error !!! Issue occurred while functioning”);

This is used when some error has occurred and you want to print some info on the console. Application did not print the expected output.

Let’s take an example to write to console.

var http = require('http');
console.error("First log on console");
http.createServer(function (request, response) {
    response.writeHead(200, {'Content-Type': 'text/plain'});
    response.write('First Demo NodeJs Program');
    response.end();
}).listen(8082);

1.) Save the above program in demonode.js and run it through console as below :
node demonode.js

2.) Hit the url http://localhost:8082/ in the browser

3.) It will display as below in the browser:
First Demo NodeJs Program

and in the console log : First log on console






Leave a Reply