NodeJs – Create Web Server

What is Node web server ?

In general ,web server handles all the Http requests for the web application.For example : ASP.NET uses the IIS webserver and PHP or java uses the Apache web server. Nodejs allows you to create your own web server which can handle the Http requests asynchronously.

Create Node Js web server

var http = require('http'); 
// 1 - Import Node.js Http module
var server = http.createServer(function (req, res) {   
// 2 - creating server
    res.writeHead(200, { 'Content-Type': 'text/html' });
    // set response header
    res.write('<html><body><p>Successfully started a web server</p>
    // set response content    
//3 - listen for any incoming requests
console.log('Node.js web server running at port 5050')

Save the above code in the file name of server.js in your project folder.Run the below command to see your results.

D:\node project >node server.js
Node.js web server running at port 5050

To see your server responsed content in web browser http://localhost:5050

What is console.log ?

The console Module provides a simple way to debug your data that is same as javascript console object provided by web browser.You can use Console class with some method such as console.log(),Console.error()console.warn() to print the informational,warning and error messages.

Next Chapter >> Connect With MySql

Leave a Reply

Your email address will not be published. Required fields are marked *