Using the Node.JS API to handle basic server

In this article, we will discuss about using the Node.JS API to handle basic server functions like GET and POST. An even simpler way to write server code is through a framework called express. I will write tutorials about that in the coming articles when we get the core idea on handling basic web server functions

To create a web server with the HTTP module in the Node JS engine, we need to use the following method:

http.createServer(callback)

Here, the callback will take two arguments; a request and a response. The request argument will send data from the client to the server so that the data can be processed, after which the response is sent to the client from the server.

The most basic example can be:

Source

The code can even be further shortened. You can replace res.write() and res.end() with :

res.end('Hello World') //Write response AND then end it 

Run the code through the command line. If you don’t get any words printed on the terminal, that’s normal. You should get your result by going to your browser and typing:localhost:3000

And the words ‘Hello World’ will be printed out on the screen


Specifying The Header

response.writeHead() sends header response to the request. Or used to set the status code and create HTTP headers.

If you want to specify the server response to be displayed in HTML. You need to tell the header that the content type should be HTML

http.createServer((req,res)=> {
res.writeHead(200,{'Content-Type':'text/html',
'Content-Length':body.length})

..

The first parameter of the res.writeHead() is the response code. It can be 2xx3xx,4xx, or 5xx.

The second parameter is the options. Here we specified that the content type is HTML i.e our response will be displayed in HTML format, and that the length of the content will be equal to the length of your body.

You can even change headers using the response argument properties:

response.writeHead = 400

HTTP Server Requests

The req parameter of the http.createServer method has all the information about the incoming request for eg. request payload, headers, URL etc.

The req parameter has the following properties

  • request.headers: Information about the request headers such as Connection, Host, Authorization, etc.
  • request.method : Information about the incoming request methods such as GET, POST, PUT, DELETE, OPTIONS, HEAD, etc.
  • request.url : Information about the incoming request URL, such as /accounts, /users, /messages etc

Example

Source

Processing Incoming Request Body in the Server

Let’s say that the client has sent data, how do we process it?

Example

Source

Executing this code requires a bit different way:

If you are on Windows, first you need to install a tool called cURL and then run the code through the command line.

To send data, open another command line window, and write:

curl -x POST -d “key-value” localhost:3000

Another alternative to cURL is Postman

On your previous command line window, you will find the data that you sent through cURL. In this case, it is key-value


Further Resources

In the next article, we’ll use Express to make our request handling not only easier, but have shorter lines of code in the process.

That’s all for today. Thank you for making it to the end!

Stay home, stay safe!

0 Shares:
Leave a Reply

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