Image for post
Image for post
Photo by Caspar Camille Rubin on Unsplash

Use Node.JS as a Web Server

HTTP Server with Core HTTP in Node.JS

Hussain Arif
Apr 23, 2020 · 3 min read

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 (update: I’ve used express framework here.) 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:

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 2xx, 3xx,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 parameter has the following properties

  • : Information about the request headers such as Connection, Host, Authorization, etc.
  • : Information about the incoming request methods such as GET, POST, PUT, DELETE, OPTIONS, HEAD, etc.
  • : Information about the incoming request URL, such as 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

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!

Easyread

Easy read, easy understanding.

Thanks to Iman Tumorang and Eko Simanjuntak

Hussain Arif

Written by

Programming enthusiast in Karachi,Pak. Happy to share whatever I learn. Instagram:hussainn.arif email: hussainarifkl [at] gmail [dot] com

Easyread

Easyread

Easy read, easy understanding. A good writing is a writing that can be understood in easy ways

Hussain Arif

Written by

Programming enthusiast in Karachi,Pak. Happy to share whatever I learn. Instagram:hussainn.arif email: hussainarifkl [at] gmail [dot] com

Easyread

Easyread

Easy read, easy understanding. A good writing is a writing that can be understood in easy ways

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store