What is express?
Express is a fast, unopinionated, minimalist web framework for Node.js. It is designed for building web applications and APIs. It simplifies the server creation process that is already available in Node.js and provides many features that make web application development fast and easy.
What are express's main functionalities?
Middleware
Middleware functions are functions that have access to the request object (req), the response object (res), and the next middleware function in the application’s request-response cycle. These functions can perform any operations, execute any code, modify the request and response objects, and end the request-response cycle.
app.use((req, res, next) => {
console.log('Middleware function');
next();
});
Routing
Routing refers to determining how an application responds to a client request to a particular endpoint, which is a URI (or path) and a specific HTTP request method (GET, POST, etc.). Each route can have one or more handler functions, which are executed when the route is matched.
app.get('/users/:userId', (req, res) => {
res.send('User ' + req.params.userId);
});
Serving Static Files
Express provides a built-in middleware express.static to serve static files, such as images, CSS, JavaScript files, etc. You simply need to pass the name of the directory where you keep your static assets to the express.static middleware to start serving the files directly.
app.use(express.static('public'))
Template Engines
Express allows you to set up template engines to dynamically render HTML on the server. It's a way to define HTML templates and inject data into them before sending them to the client.
app.set('view engine', 'pug');
app.get('/', (req, res) => {
res.render('index', { title: 'Hey', message: 'Hello there!' });
});
Error Handling
Express provides a way to handle errors via middleware. You can define error-handling middleware functions in the same way as other middleware functions, except with four arguments instead of three, specifically with the signature (err, req, res, next).
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).send('Something broke!');
});
Other packages similar to express
koa
Koa is a new web framework designed by the team behind Express, which aims to be a smaller, more expressive, and more robust foundation for web applications and APIs. Koa uses async functions to eliminate callback hell and simplify error handling. It does not bundle any middleware within its core, and it provides an elegant suite of methods that make writing servers fast and enjoyable.
hapi
Hapi is a rich framework for building applications and services. It enables developers to focus on writing reusable application logic instead of spending time building infrastructure. Hapi is known for its powerful plugin system and comprehensive API. Compared to Express, Hapi has a more rigid and structured approach, which can be beneficial for larger teams and projects.
fastify
Fastify is a web framework highly focused on providing the best performance out of the box. It is inspired by Express and Hapi but is built with a focus on speed and low overhead. Fastify is designed to be as fast as possible, and it achieves this by leveraging Node.js features and asynchronous patterns.
sails
Sails.js is a modern MVC framework for Node.js that is built on top of Express. It is designed to emulate the familiar MVC pattern of frameworks like Ruby on Rails but with support for the requirements of modern apps: data-driven APIs with scalable, service-oriented architecture. Sails is especially good for building data-heavy enterprise-grade apps.
Fast, unopinionated, minimalist web framework for node.
const express = require('express')
const app = express()
app.get('/', function (req, res) {
res.send('Hello World')
})
app.listen(3000)
Installation
This is a Node.js module available through the
npm registry.
Before installing, download and install Node.js.
Node.js 0.10 or higher is required.
If this is a brand new project, make sure to create a package.json
first with
the npm init
command.
Installation is done using the
npm install
command:
$ npm install express
Follow our installing guide
for more information.
Features
- Robust routing
- Focus on high performance
- Super-high test coverage
- HTTP helpers (redirection, caching, etc)
- View system supporting 14+ template engines
- Content negotiation
- Executable for generating applications quickly
PROTIP Be sure to read Migrating from 3.x to 4.x as well as New features in 4.x.
Quick Start
The quickest way to get started with express is to utilize the executable express(1)
to generate an application as shown below:
Install the executable. The executable's major version will match Express's:
$ npm install -g express-generator@4
Create the app:
$ express /tmp/foo && cd /tmp/foo
Install dependencies:
$ npm install
Start the server:
$ npm start
View the website at: http://localhost:3000
Philosophy
The Express philosophy is to provide small, robust tooling for HTTP servers, making
it a great solution for single page applications, websites, hybrids, or public
HTTP APIs.
Express does not force you to use any specific ORM or template engine. With support for over
14 template engines via Consolidate.js,
you can quickly craft your perfect framework.
Examples
To view the examples, clone the Express repo and install the dependencies:
$ git clone git://github.com/expressjs/express.git --depth 1
$ cd express
$ npm install
Then run whichever example you want:
$ node examples/content-negotiation
Contributing
The Express.js project welcomes all constructive contributions. Contributions take many forms,
from code for bug fixes and enhancements, to additions and fixes to documentation, additional
tests, triaging incoming pull requests and issues, and more!
See the Contributing Guide for more technical details on contributing.
Security Issues
If you discover a security vulnerability in Express, please see Security Policies and Procedures.
Running Tests
To run the test suite, first install the dependencies, then run npm test
:
$ npm install
$ npm test
People
The original author of Express is TJ Holowaychuk
The current lead maintainer is Douglas Christopher Wilson
List of all contributors
License
MIT