Backend development part 1 [Node.js Server]

Share

Before starting with node.js development we need to know what exactly node.js is.

Node.js is an open-source, cross-platform back-end JavaScript runtime environment that executes JavaScript code outside of a web browser and runs on a JavaScript Engine (i.e., V8 engine). It was created to construct scalable network applications.

Let’s forge ahead and see what step we should follow in order to create a Node Server.

Prerequisite

  1. Node Installed
  2. JS Package Manager (npm or yarn)
  3. Browser
  4. Code Editor (VS Code)
  5. Basic understanding of JS

Node Installation

There are installation instructions for Node.js on the official website: https://nodejs.org .

Download an installation file according to your environment and run it. After installation, ensure it is appropriately installed and available in CLI tools (Command Line, Terminal). To check it, run node -v in your CLI. it should show the node version in the output.

Node Version

Project Initialization

To run the node server, we must create a server file along with a package file to install the js package which helps to create a web server. For this series of articles, I have created a folder named backend. After creating the folder, open it in VS Code ( Code Editor). Now it is time to set up a node js project. To do so, run npm init command in the terminal opened in the same directory.

You would be asked a series of questions about the project.

project initialization

first is the package name, it is the name for the project, we just keep it simpler. version option can be kept default. To keep it default, just press enter. options that we customize are the entry point and test command. In the entry point, we can assign our custom file name which will be executed to start the node server. In the test command, we assign complete command to run a js file using node in terminal (run js outside the browser). This process will create a file name package.json in the working directory.

Creating A Basic Server

Now it’s time to create a basic node js server. To do so, we first create a file named index.js.

const http = require('http');

const requestListener = function (req, res) {
  res.writeHead(200);
  res.end('Hello, World!');
}

const server = http.createServer(requestListener);
server.listen(5000,()=>console.log("+ Server Started\n+ Port: 5000"));

We first import a package named http then we call a function createServer() with a callback function that is responsible for listening to an HTTP request. In requestListener function, we send a status code 200 in the HTTP response header with content Hello, World! To run a node server, we run a command : npm run test in terminal opened in the same directory. It will start a server on port 5000.

Node server running on port 5000

Now, we can access the server on localhost:5000 or 127.0.0.1:5000 . Generally, we test this HTTP API in the browser, or you can use curl in the terminal to test in the terminal itself.

IN BROWSER: just open a new tab and enter the URL : localhost:5000 or 127.0.0.1:5000

Accessing server in browser
Accessing the server using curl

if for any reason it failed to start a server. try installing http package on your machine by using the command: npm install -g http and try restarting the server. This should solve an issue.

This is a basic node.js server. To develop a backend for a web application, we use Express.js which is a web Node web framework. To use Express use just install the express package, that’s it.

Stay tuned for all parts of this series!!!!

Other JS Articles: Javascript

Share
Sudeep Mishra

Sudeep Mishra

Healing

%d bloggers like this: