How to create a Web-Server with core inbuild HTTP node module | Must know before learning ExpressJS
Express JS is a node module used to create web servers and APIs in a much easier and cleaner way other than using an inbuild HTTP node module(by this you can create a web server without express). But before directly jumping into learning expressjs you should at least look at the basics of the inbuild HTTP node module for creating web servers to see how things work under the hood and what are some problems that are being solved by using expressjs.
I am here assuming that you are familiar with JavaScript and the basics of nodejs.
Let's first understand how the Client-Server relationship works
Client-Server relation works with HTTP (HyperText Transfer Protocol) which is nothing but just a set of rules by which Client-Server relation is bounded. So whenever a client sends the request either to get data from the server with the GET
method or to post data on the server with the POST
method. In any of these two cases, the server will respond to that particular request either by providing or posting the desire data with a specific success code or by a specific error code in case of any error. So this is nothing but a connection of the client to server to make a request and get a response from the server.
Creating our very first webserver
Now you are familiar with how the client-server relationship works so let's create a simple webserver directly to our computer with an inbuild HTTP module of nodejs.
1. Setting up the dev environment
On your favorite IDE, create a folder with any name you want then open that folder in terminal/cmd and initialize the project by typing in your terminal/cmd npm init
.
If you don't know these things and directly reading this blog then my friend coding is not meant for you ๐. JK, try googling how to initialize the project with npm init
and figure out how to do this.
2. Creating an index.js file
Create an index.js file.
3. Now let's code our very first web server
const http = require('http');
//create a server object:
const server = http.createServer(function (req, res) {
console.log('server has started')
})
server.listen(8080); //the server object listens on port 8080
Let's understand what's going on in the above code
In the first line, we're requiring the code HTTP module of the nodejs so that we can use this to create a web server.
In the second line, we are using the createServer
method of the HTTP module for creating a server. Now, createServer will take two parameters req
and res
req stand for a request which means what the client is sending to the server and res stand for respond which means what the client is getting in return to that request.
For example, if the user wants to log in to a website then he will send credentials to the server via a form and the server will verify those credentials and then in return server will send an authorization token to log that user in.
Let's now send a response as a string
const http = require('http');
//create a server object:
const server = http.createServer(function (req, res) {
res.write('Hello World!')
res.end(); //end the response
})
server.listen(8080); //the server object listens on port 8080
res.write()
will be used to send data to the client in this case we are sending a string "Hello World!". And we also have to use res.end()
to end the response otherwise it will never end.
Sending the JSON data from the server
const http = require('http');
//create a server object:
const server = http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'application/json'});
res.write(JSON.stringify({message: "hello World!"})); //write a response to the client
res.end(); //end the response
})
server.listen(8080); //the server object listens on port 8080
Before this let's understand what headers are.
Headers
Headers are part of APIs( Application Programmable Interfaces ) that helps you to send and receive additional informations with data. For example, in this case, we are specifying data type as JSON.
We are using res.writeHead()
to add headers with the response. 1st parameter is a status code that we will send with this response, in this case, it's 200 with means success. And as a second parameter, we are specifying the content type as JSON.
And finally, at res.write()
we also have to parse json object to string with JSON.stringify()
.
That's it. Congratulation, you have created your very first HTTP server.
Click Here to get the code and play with it.