Node vs Express
Last Updated :
14 Jan, 2024
Node is a runtime environment for executing server-side JavaScript code, while Express is a framework built for Node, offering features and tools that streamline server-side development. Each plays a distinct role in web development.
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 Node JS 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 middleware 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 i/o applications that are server-side event-driven and made using JavaScript.
- Express JS is a framework based on Node JS which is used for building web applications using approaches and principles of Node JS event-driven architecture.
Usage |
It 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 features |
More features than Node JS. |
Fewer features. |
Building Block |
It is built on Node JS |
It is built on Google’s V8 engine. |
Written in |
JavaScript |
C, C++, JavaScript |
Framework/Platform |
Framework based on Node JS |
Run-time platform or environment designed for server-side execution of JavaScript. |
Controllers |
Controllers are provided. |
Controllers are not provided. |
Routing |
Routing is provided. |
Routing is not provided. |
Middleware |
Uses middleware for the arrangement of functions systematically server-side. |
Doesn’t use such a provision. |
Coding time |
It 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.
Below are basic examples of creating a server in Node JS & Express JS:
Express JS Server:
Step to Install Express: Install express using the following command:
npm i --save express
Javascript
const express = require( 'express' );
const app = express();
app.get( '/' , (req, res) => {
res.send( '<h2>Hello from Express.js server!!</h2>' );
});
app.listen(8080, () => {
console.log( 'server listening on port 8080' );
});
|
Steps to Run the Server: Run the index.js file using the following command:
node index.js
Output:
Node JS Server:
Require the http module using the following code:
const http = require('http');
Javascript
const http = require( 'http' );
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.listen(3000, () => {
console.log( "Server listening on port 3000" )
});
|
Steps to Run the Server:
node index.js
Output:
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.
Javascript
const express = require( 'express' );
const app = express();
app.get( '/' , (req, res) => {
res.send( '<h2>Hello from Express.js server!!</h2>' );
});
app.get( '/about' , (req,res) => {
res.send( '<h2>GeeksforGeeks- Express.js</h2>' );
});
app.listen(8080, () => {
console.log( 'server listening on port 8080' );
});
|
Steps to Run the Server: 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:
Routing in Node JS:
- Routing is not provided.
- We have to check the URL and method of the request while sending response.
Javascript
const http = require( 'http' );
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.listen(3000, () => {
console.log( "Server listening on port 3000" )
});
|
Steps to Run the Server:
node index.js
Output: Open your browser and go to http://localhost:3000/about:
Share your thoughts in the comments
Please Login to comment...