Node.js

Create Your First Basic HTTP Server Using Node.js

Google+ Pinterest LinkedIn Tumblr

Every website or web application in the world needs a web server to serve the files. A web server is a system that processes via the HTTP Protocol in response to the client request. In simple word, the main purpose of the web server is to store and transfer the website data upon the request of the client. In this article, we are going to create a simple web server using the one of the most popular and widely used technology Node.js. Node.js has built-in HTTP module to serve the data and respond to the request of the client. Before reading further this article, you must have the good understanding of the Node.js and installed it on your system. You can install Node.js by going to our previous article on Node.js.

Initialize Node.js Application Using NPM

To get started with this article, we first need to initialize the project in the project folder. You can choose any location for your project according to your Operating System. Initialize your project using the below command in the terminal

 npm init

After running the above command, its initiate a prompt to create a package.json file for your application. Type any name of your project, tell something about its description, version, an entry point of your application ( Here we are using app.js file ), and rest of the part. After filling all these details a package.json file will be created in the project root directory.

Start Writing Some Code

Now create a new file and named it which you name for your entry point file while generating your package.json file. In my case, I named it with app.js. This file is the core file of the application and serves web pages and data to the client. Copy the below code and paste it into the newly created app.js file

const http = require('http');

const server = http.createServer((req, res) => {
	res.writeHead(200);
	res.end('Nice Job! you created your first http server');
});

server.listen(3010, ()=>{
	console.log('the server is running on http://localhost:3010');
});

Above code is the minimal code to create HTTP server in the node.js. You can now run the above code by typing the below command in the terminal

node app.js

After running the above command, if everything is fine then you will see a message in the terminal saying that the server is running on http://localhost:3010

Go to your browser and type http://localhost:3010 in the URL you can see message like Nice Job! you created your first HTTP server. Its create a mini web server which sends a “Nice Job! you created your first HTTP server”  message every time when URL is called.

node-http-codesquery

Running Some HTML Code

Till now we create a simple HTTP web server which sends the only simple plain text to the client on every request. Let’s move one step further and learn how to send HTML response with the web server. Let’s modify our previous code to send the HTML response. We need to add the additional setting that indicates the MIME type of the response. There is so many MIME type which a server can send to the client. Here, we use “text/html” MIME type. So After modifying the code now, Our code looks like below

const http = require('http');

const server = http.createServer((req, res) => {
	res.writeHead(200 ,  {"Content-Type": "text/html"});
	res.end('<p><strong>Nice Job!<strong><p> you created your first http server with <strong>HTML<strong> as a response.');
});

server.listen(3010, ()=>{
	console.log('the server is running on http://localhost:3010');
});

In the above code, we added some HTML tag and MIME type with the response. Now restart your node server and go to the browser, you will see HTML response in the browser like below image

node-http-html-response

Conclusion

So we learn how to send plain and HTML response using Node.js HTTP web server but as a developer or in any web application development we do not want to write code in such a messy and ugly way. Node.js HTTP module is a very low level and to create a complex web application using the Node.js is very time-consuming. This is the reason why everyone using a framework to build a web application. There is so many web framework available for the node.js development which you can pick according to your requirements.

Write A Comment

Pin It