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. These days node.js server is so much popular and every developers and company want to use it.

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 node.js server.  Before reading further this article, you must have a good understanding of the Node.js and installed it on your system. You can install Node.js by going to the previous article on Node.js.

Node.js has built-in HTTP module to serve the data and respond to the request of the client. We will learn in details why the node.js server is so much popular these days and how to set up one for yourself.

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, you will see 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 a 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 an HTML response with the web server. Modify the previous code to send the HTML response.

We also 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 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.

The 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.

Now you know how to start your node.js server and its time to learn more about the node.js.

Following are the tutorials which you should start reading after this.

  1. Learn the basic concept of the ES6 features which saves lots of your time during the coding.
  2. How to use node.js Express framework to build node.js application more easily.
  3. Connect Node.js with MongoDB Using Mongoose
  4. Build a secure and scalable node.js RESTful API

If you have any question then let me know in the comment section.

If you like this article then share it because sharing is caring. 🙂

Pin It