Home >>Nodejs Tutorial >Node.js First Example
Node.js applications can be console-based and web-based.
console.log ('Hello phptpoint');
you can run the codeby open Node.js command prompt in your system:
Here, console.log () function displays message on console.
A web server node.js has contains three sections:
Follow these steps:
var http = require("http");
http.createServer(function (request, response) {
// Send the HTTP header
// HTTP Status: 200 : OK
// Content Type: text/plain
response.writeHead(200, {'Content-Type': 'text/plain'});
// you can send the response body as "Hello World"
response.end('Hello World\n');
}).listen(8081);
// Console will print the message
console.log('Server running at http://127.0.0.1:8081/');
File: main.js
var http = require("http");
http.createServer(function (request, response) {
// Send the HTTP header
// HTTP Status: 200 : OK
// Content Type: text/plain
response.writeHead(200, {'Content-Type': 'text/plain'});
// sending “hello world” as the response body
response.end('Hello World\n');
}).listen(8081);
// Console will print the message
console.log ('Server running at http://127.0.0.1:8081/');
You can go to the Start menu and Click the Node.js command prompt
Now command prompt is open:Path setting: here, we have saved the " main.js" file to the desktop here.
So, you can type the cd desktop on the prompt for the instruction.
Before that you can run the main.js file to get the server started as follows:
node main.js
Now server is started.
Open http://127.0.0.1:8081/ in any browser. You will see the following result.
However, you will run the " main.js node" command once again, if you make any changes to the " main.js" file