Node.js vs Express.js

20 Jun, 2022
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, Walmart 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.
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 moduleconst express = require('express');const app = express(); // Route handlingapp.get('/', (req, res) => {    res.send('<h2>Hello from Express.js server!!</h2>');}); // Server setupapp.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 moduleconst http = require('http'); // Creating server objectconst 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 setupserver.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 moduleconst express = require('express');const app = express(); // Handling '/' requestapp.get('/', (req, res) => {    res.send('<h2>Hello from Express.js server!!</h2>');}); // Handling '/about' requestapp.get('/about', (req,res) => {    res.send('<h2>GeeksforGeeks- Express.js</h2>');}); // Server setupapp.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 moduleconst http = require('http'); // Creating server objectconst 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 setupserver.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:
vinayedula
Express.js
Node.js-Misc
Technical Scripter 2020
Difference Between
JavaScript
Node.js
Technical Scripter
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.