Introduction to NodeJS Coding - Part 2

Here we gonna create a simple HTTP server with REST capabilites. Hope you guys followed my previous introductory coding blog on NodeJS. If not click Here ,

First let's see what is a Web Server,

A Web server is a program that uses HTTP (Hypertext Transfer Protocol) to serve the files that form Web pages to users, in response to their requests, which are forwarded by their computers' HTTP clients. Dedicated computers and appliances may be referred to as Web servers as well. [whatIs.com]


So, let's create one,
  • First create a JavaScript file as 'server.js'.
  • Import the http module from the core libraries.
    • const http = require('http');
  • Create a httpServer that listens to port 3000 and return HTML with Hello World text in h1 header upon GET request.


  •  Check the output of the code by running the code and accessing http://localhost:3000 in
    browser.
 So let's improve our server,
  •  adding a POST request that accepts form field name and return HTML with Hello
    {name}. 
  • Do the following changes.



  •  Run the code using following code,
    • node server.js
  •  Now, use a REST client or HTTP requester or POSTMAN to check the 'POST' method



 As you can see, using our post endpoint we can display 'Hello <Given Name>' .

So this is a very simple web server which is implemented using NodeJS. Hope you guys now have a very basic understanding in how to use NodeJS, see you in next tutorial which will be a continuation of NodeJS blog series and we will be focusing on using ExpressJS and Mongo DB to do some server side implementation.



Comments

Popular posts from this blog

Introduction to using ExpressJS with MongooseJS and NodeJS - Part 2

Using OAuth 2.0 Framework to Manipulate Files in Google Drive

Preventing Cross-Site Request Forgery (CSRF) with Double Submit Token Pattern