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.
var express = require('express');
var app = express();
app.get('/', function(req, res){
res.send('Hello World');
});
app.listen(3000);
Note that Express v4 release candidates have been released.
Please try it out, but not in production, and report any issues you may find here or to the appropriate repositories.
Be sure to read Migrating from 3.x to 4.x as well as New features in 4.x.
Installation
$ npm install express
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@3
Create the app:
$ express /tmp/foo && cd /tmp/foo
Install dependencies:
$ npm install
Start the server:
$ npm start
Features
- Robust routing
- HTTP helpers (redirection, caching, etc)
- View system supporting 14+ template engines
- Content negotiation
- Focus on high performance
- Environment based configuration
- Executable for generating applications quickly
- High test coverage
Philosophy
The Express philosophy is to provide small, robust tooling for HTTP servers, making
it a great solution for single page applications, web sites, 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.
More Information
Viewing Examples
Clone the Express repo, then install the dev dependencies to install all the example / test suite dependencies:
$ git clone git://github.com/visionmedia/express.git --depth 1
$ cd express
$ npm install
Then run whichever tests you want:
$ node examples/content-negotiation
You can also view live examples here:
Running Tests
To run the test suite, first invoke the following command within the repo, installing the development dependencies:
$ npm install
Then run the tests:
$ make test
Contributors
https://github.com/visionmedia/express/graphs/contributors
License
MIT