Building a simple server with Node.js
Node.js is a platform built on Chrome’s JavaScript runtime for easily building fast and scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.
Introduction:
Node.js is an open-source and cross-platform runtime environment for executing JavaScript code outside a browser. You need to remember that NodeJS is not a framework, and it’s not a programming language. Node.js is mostly used in server-side programming. In this article, we will discuss how to make a web server using node.js.
Creating Web Servers Using NodeJS
There are mainly two ways as follows.
- Using http inbuilt module
- Using express third party module
Using http module :
HTTP and HTTPS, these two inbuilt modules are used to create a simple server. The HTTPS module provides the feature of the encryption of communication with the help of the secure layer feature of this module. Whereas the HTTP module doesn’t provide the encryption of the data.
Project structure: It will look like this.
const http = require("http")
const server = http.createServer((req, res) => {
// Sending the response
res.write("This is the response from the server")
res.end();
})
// Server listening to port 3000
server.listen((3000), () => {
console.log("Server is Running");
})
Run index.js file using below command:
node index.js
Output: Now open your browser and go to http://localhost:3000/, you will see the following output:
This is the response from the server
Using express module :
The express.js is one of the most powerful frameworks of the node.js that works on the upper layer of the http module. The main advantage of using express.js server is filtering the incoming requests by clients.
Installing module: Install the required module using the following command.
npm install express
Project structure : It will look like this.
// Importing express module
const express = require("express")
const app = express()
// Handling GET / request
app.get("/", (req, res, next) => {
res.send("This is the GET request")
})
// Handling POST/ request
app.post("/", (req, res, next) => {
res.send("This is the POST request");
})
// Server setup
app.listen(3000, () => {
console.log("Server is Running")
})
Run the index.js file using the below command:
node index.js
Output: Now open your browser and go to http://localhost:3000/, you will see the output.
You can also add middleware functions to perform certain actions before or after a request is handled.
app.use((req, res, next) => {
console.log(`Received ${req.method} request for ${req.url}`)
next()
})
This is a simple example of how to build a server with Node.js.It is worth noting that the above example is a very basic one, and in real-life application you will need to handle different errors, handle security, enable CORS, handle session or token based auth, handle different HTTP methods, etc. The best practice is also to separate your server logic and routes in different files.
Node.js is often used to build server-side web applications in Organizations. One of such ecommerce development company in delhi is scrrum labs located in New Delhi which uses NodeJs .
One of the main advantages of using Node.js is that it allows for the use of a single language (JavaScript) for both client-side and server-side code, making it easier for developers to work on both parts of an application.Node.js is commonly used to build real-time applications such as Mobile Application Development ,chat applications, online gaming, and other interactive applications that require a low-latency connection between the client and the server.