Skip to content
Related Articles

Related Articles

Node.js vs Express.js
  • Last Updated : 09 Dec, 2020

Node.js: Node.js is an open source and cross-platform runtime environment for executing JavaScript code outside of a browser. You need to remember that NodeJS is not a framework and it’s not a programming language. Most of the people are confused and understand it’s a framework or a programming language. We often use Node.js for building back-end services like APIs like Web App or Mobile App. It’s used in production by large companies such as Paypal, Uber, Netflix, Wallmart and so on.

Express.js: Express is a small framework that sits on top of Node.js’s web server functionality to simplify its APIs and add helpful new features. It makes it easier to organize your application’s functionality with middle ware and routing. It adds helpful utilities to Node.js’s HTTP objects. It facilitates the rendering of dynamic HTTP objects.

Difference between Node.js and Express.js:

  • Node.js is a platform for building the i/o applications which are server-side event-driven and made using JavaScript.
  • Express.js is a framework based on Node.js for which is used for building web-application using approaches and principles of Node.js.event-driven.
FeatureExpress.jsNode.js
UsageIt is used to build web-apps using approaches and principles of Node.js. It is used to build server-side, input-output, event-driven apps.
Level of featuresMore features than Node.js.Fewer features.
Building BlockIt is built on Node.js.It is built on Google’s V8 engine.
Written inJavaScriptC, C++, JavaScript
Framework/PlatformFramework based on Node.js.Run-time platform or environment designed for server-side execution of JavaScript.
ControllersControllers are provided.Controllers are not provided.
RoutingRouting is provided.Routing is not provided.
MiddlewareUses middleware for the arrangement of functions systematically server-side.Doesn’t use such a provision.
Coding timeIt requires less coding time.It requires more coding time.

Example: Following comparison shows how the same code is written differently in Node.js (Left tab code) and Express.js (Right tab code). 

Note: These codes are covered in the below comparisons.



Starting a server in Node.js & Express.js
1. Express.js Server: Install express using the following command:

npm install --save express

Filename: index.js

Javascript




// Requiring the module
const express = require('express');
const app = express();
  
// Route handling
app.get('/', (req, res) => {
    res.send('<h2>Hello from Express.js server!!</h2>');
});
  
// Server setup
app.listen(8080, () => {
    console.log('server listening on port 8080');
});

Run the index.js file using the following command:

node index.js

Output:

2. Node.js Server: Require the http module using the following code:



const http = require('http');

Filename: index.js

Javascript




// Requiring the module
const http = require('http');
  
// Creating server object
const server = http.createServer((req, res) => {
    res.setHeader('Content-Type', 'text/html');
    res.write('<html>');
    res.write('<head><title>GeeksforGeeks</title><head>');
    res.write('<body><h2>Hello from Node.js server!!</h2></body>');
    res.write('</html>');
    res.end();
});
  
// Server setup
server.listen(3000, ()=> {
    console.log("Server listening on port 3000")
});

Run the index.js file using the following command:

node index.js

Output:

Routing in Node.js & Express.js
1. Routing in Express.js:

  • Routing is provided & implementation is easy.
  • We can directly pass the route name and function to the inbuilt function of express, mentioning the type of request as get, post.

Filename: index.js

Javascript




// Requiring module
const express = require('express');
const app = express();
  
// Handling '/' request
app.get('/', (req, res) => {
    res.send('<h2>Hello from Express.js server!!</h2>');
});
  
// Handling '/about' request
app.get('/about', (req,res) => {
    res.send('<h2>GeeksforGeeks- Express.js</h2>');
});
  
// Server setup
app.listen(8080, () => {
    console.log('server listening on port 8080');
});

Run the index.js file using the following command:

node index.js

Open your browser and go to http://localhost:8080/about, following will be the output:

2. Routing in Node.js:

  • Routing is not provided.
  • We have to check the URL and method of the request while sending response.

Filename: index.js

Javascript




// Requiring the module
const http = require('http');
  
// Creating server object
const server = http.createServer((req, res) => {
    const url = req.url;
      
    if(url === '/') {
        res.write('<html>');
        res.write(
'<head><title>GeeksforGeeks</title><head>');
        res.write(
'<body><h2>Hello from Node.js server!!</h2></body>');
        res.write('</html>');
        return res.end();
    }
      
    if(url === '/about') {
        res.write('<html>');
        res.write(
'<head><title>GeeksforGeeks</title><head>');
        res.write(
'<body><h2>GeeksforGeeks- Node.js</h2></body>');
        res.write('</html>');
        return res.end();
    }
});
  
// Server setup
server.listen(3000, () => {
    console.log("Server listening on port 3000")
});

Run the index.js file using the following command:

node index.js

Open your browser and go to http://localhost:3000/about, following will be the output:

My Personal Notes arrow_drop_up
Recommended Articles
Page :