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:
- handle routing
- handle request and response objects
- handle middleware
- several other features which make it easier to build web apps and APIs on top of Node.js.
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}`) });