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.
Express
Insanely fast (and small) server-side JavaScript web development framework
built on node and Connect.
var app = express.createServer();
app.get('/', function(req, res){
res.send('Hello World');
});
app.listen(3000);
Installation
$ npm install express
Features
- Robust routing
- Redirection helpers
- Dynamic view helpers
- Content negotiation
- Focus on high performance
- View rendering and partials support
- Environment based configuration
- Session based flash notifications
- Built on Connect
- High test coverage
- Executable for generating applications quickly
- Application level view options
Via Connect:
- Session support
- Cache API
- Mime helpers
- ETag support
- Persistent flash notifications
- Cookie support
- JSON-RPC
- Logging
- and much more!
Contributors
The following are the major contributors of Express (in no specific order).
More Information
Node Compatibility
Express 1.x is compatible with node 0.2.x and connect < 1.0.
Express 2.x is compatible with node 0.4.x and connect 1.x
License
(The MIT License)
Copyright (c) 2009-2011 TJ Holowaychuk <tj@vision-media.ca>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.