Introduction
Express js is a popular Node.js web application framework that provides a robust set of features for building web applications and APIs. It simplifies the process of handling HTTP requests, routing, middleware, and more.
Setting up Express js
To get started with Express js, you first need to install it using npm. You can create a new Express app by running npm init
and then installing Express using npm install express
. Once installed, you can create a new Express app by requiring the express module and setting it up.
Creating Routes
In Express js, routes are used to determine how your application responds to client requests. You can define routes using the app.get()
, app.post()
, app.put()
, and app.delete()
methods. Here’s an example of defining a simple route that responds with “Hello, World!”:
1
2
3
app.get('/', (req, res) => {
res.send('Hello, World!');
});
Middleware
Middleware functions in Express js are functions that have access to the request and response objects in the application’s request-response cycle. They can perform tasks such as logging, parsing request bodies, and more. You can use middleware with the app.use()
method. Here’s an example of a simple logging middleware:
1
2
3
4
app.use((req, res, next) => {
console.log('Request received at:', Date.now());
next();
});
Error Handling
Express js provides built-in error handling middleware that can be used to handle errors that occur during the request-response cycle. You can define error-handling middleware using a function that takes four arguments - err
, req
, res
, and next
. Here’s an example of error handling middleware:
1
2
3
4
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).send('Something broke!');
});
Conclusion
Working with Express js allows you to quickly build powerful web applications and APIs in JavaScript. By leveraging its features such as routing, middleware, and error handling, you can create efficient and reliable applications. Its popularity and active community make it a great choice for web development projects.