Table of Contents

Node.js use Express.js

About

Express.js, or simply Express, is a web application framework for Node.js. It was designed for building web applications and APIs. Express is known for its simplicity, flexibility, and performance. It is part of the MEAN (MongoDB, Express.js, AngularJS, Node.js) and MERN (MongoDB, Express.js, React, Node.js) stacks, which are popular technology stacks for building modern full-stack web applications.

We'll use Express.js as a web server implementation for Node.js. It simplifies many of the complexities of setting up a server in Node.js.
Express provides mechanisms to:

Express.js does not replace the built-in HTTP server in Node.js, but rather works on top of it, providing a higher-level, more feature-rich interface for building web applications.
While Express can serve static files (like HTML, CSS, and JavaScript files), it's often used as an API server that communicates with a front-end application built with a library or framework like React, Angular, or Vue.js. This allows you to create full-stack JavaScript applications where both the front-end and the back-end are written in JavaScript.

Express.js is not the only option for building web servers in Node.js. Other popular options include Koa.js, Hapi.js, and Fastify, each with their own strengths and use cases.

Installation

You must have Node.js and npm (Node Package Manager) installed on your computer to use Express.js. You would then install Express with the command

npm install express

Very basic example

express.js
const express = require('express');
const app = express();
const port = 3000;
 
app.get('/', (req, res) => {
  res.send('Hello World!')
});
 
app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
});