HTTP server

We will make use of the Express framework, which is one of the most popular web frameworks used with Node.js. Please take this as an exercise to learn the Express.js web framework. Follow these steps and at the end, you will have the HTTP server up and running:

  1. Since our server is a 64-bit Windows machine, we will download the latest Node.js installer from the official website: https://nodejs.org/en/download/. You can install Node.js as per the server configuration and OS.
  2. After downloading the installer, run it and complete the installation. The installer will also download Node Package Manager (NPM) which is used to download and manage additional packages for Node.js development.
  3. To check the successful installation of the node and npm, execute the following command in the Command Prompt:
node -v
  1. This will give you version of Node.js installed:
npm -v
  1. This will print the version of npm installed.
  2. Now, create a folder with the name HttpServer, where we will place our code files.
  3. Open Command Prompt as an administrator and move to the HttpServer folder:
  1. To make sure we have our environment set up at the local folder level, run the following command:
npm init
  1. It will help us create package.json. This file contains all the metadata relevant to the Node project. A few questions will be asked in order to set up package.json, as shown in the following screenshot:
  1. To install node modules, run the following command:
 npm install

This will install node modules at the local folder level.

  1. Now we are ready to write our HTTP server code. Create a file, server.js, in the HttpServer folder and add these lines of code:
const express = require('express');
const app = express();

We include/import the express and body-parser modules in the server.js file. Express is a web framework used to create webserver and API endpoints.

Merely including modules in code doesn't serve the purpose; we must explicitly install these modules using the npm command as shown here:

npm install express -save
const server = app.listen(8080, function(){
var host = server.address().address;
var port = server.address().port;
console.log ("Example app listening at http://%s:%s", host, port);
})
  1. Here, we provide a port number on which our HTTP server will listen, and also this to on the console.
  2. Now, we will create an API route/path, which will act as the endpoint for the client to reach the server. We will use HTTP GET and POST:
app.get('/get/information',function(req,res){
console.log("get request received from client") ; res.send("success") ;
}
  1. In the GET method, the path or endpoint is /get/information. Upon receiving the request from the client on this path, we will print a message on the console and send a response message, success, to the client, as a confirmation of the receipt of the request.
  2. Let's run the code. Open the Command Prompt, move to the folder (HttpServer in our case) where we have the code file, and execute following command:
node server.js
  1. This is what the actual output upon successful running of your code will look like in Command Prompt:
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.186.164