Home Blog Creating a Server using NodeJS vs Express
Creating a Server using NodeJS vs Express

Creating a server using NodeJS vs Express

There are over 80 frameworks in JavaScript. NodeJS is one of the most famous and powerful frameworks across the world. NodeJS is majorly used to create event-driven real-time applications. One of the most important modules of NodeJS is HTTP.


Using the HTTP module you can create web servers easily. You can handle all the HTTP requests asynchronously using the module.


Now since we are talking about web servers it becomes essential to discuss about “Express” as well.


Express JS is actually a NodeJS framework that is used to make web and mobile applications quite smoothly. Developing single-page, multi-page, and hybrid applications based on API and web servers at a very fast pace and with ease with Express. Express makes NodeJS easier. 


Many big companies including Netflix, IBM, Uber, etc. rely on Express JS. NodeJS and Express are not entirely comparable since they serve different purposes. 


Some functionality that makes Express better than NodeJS includes:

1. Middlewares: These are the functions that execute by the time HTTP requests are served by the application.

2. Routing: Routing in NodeJS seems a little more complex than in Express.

3. Speed and Efficiency: Express JS is considered as faster and even more efficient than NodeJS.




Now let’s see how creating a server using Express is different and better than using NodeJS:


Server using NodeJS

const http = require('http');
const server = http.createServer((req, res) => {
if(req.url==’/’){
    res.writeHead(200, { 'Content-Type': 'text/plain' });
    res.end('Hello this is home’);
}
else if(req.url==’/about’){
 res.writeHead(200, { 'Content-Type': 'text/plain' });
    res.end(' This is about page ');
}
else {
 res.whitehead(404, { 'Content-Type': 'text/plain' });
    res.end('Server Not Found ');
}
});
const port = 3000;
server.listen(port, () => {
    console.log(`Server is listening on port ${port}`);
});

Using require(‘http’) we are importing http module. http.createServer() function is taking 2 parameters req and res in the callback. The server is sending the request and is getting back the response. There are 2 URLs mentioned for the routing. res.writeHead(404, { 'Content-Type': 'text/plain' }) is used to return the server not found response. 


Server using ExpressJS

const express = require('express');
const app = express();
const port = 3000;
app.get('/', (req, res) => {
    res.send(' This is homepage ');
});
app.get('/about', (req, res) => {
    res.send(' This is about page ');
});
app.listen(port, () => {
    console.log(`Server is listening on port ${port}`);
});

Using require(‘express’) we are importing the express module and we are passing the express() to the app variable. We can perform HTTP methods easily with ‘app.get’, ‘app.post’, etc. As you can see there is no if else ladder we have to use like in NodeJS. Routing becomes easier with Express. 




So, in this article we came across how we can create a server using both NodeJS and ExpressJS. Setting up a server is same in both the cases, however Express is used majorly to ease up the routing, handling requests and managing other aspects of web application. It is recommended to use both NodeJS and Express together, NodeJS to set up the environment and Express to handle the HTTP requests.

Contact us 

Get In Touch with Us

Leave a Comment

Your email address will not be published.

Submit
Your comment is under review by our moderation team.