Skip to main content

Introduction to Express.js

Introduction to Express.js

Express.js, or simply Express, is a web application framework for Node.js. It is designed for building web applications and APIs. It is the standard server framework for Node.js, and it simplifies the process of writing server code. In this tutorial, we'll cover the basics of Express.js, including installation, routing, middleware, error handling and serving static files.

Installing Express.js

Before we start, you should have Node.js and npm (node package manager) installed on your computer. If not, you can download and install them from here.

To install Express.js, open your terminal and type:

npm install express

This command installs Express in your current directory and adds it to the dependencies in your package.json file.

Hello, Express

Let's start by building a simple Express app that responds with "Hello, Express!" when anyone visits your website.

Create a new file named app.js and add the following code:

const express = require('express');
const app = express();

app.get('/', function (req, res) {
res.send('Hello, Express!');
});

app.listen(3000, function () {
console.log('Express app listening on port 3000!');
});

To run this app, go back to your terminal and type:

node app.js

If you open your web browser and navigate to http://localhost:3000, you should see the message "Hello, Express!".

Routing

In Express.js, routing refers to defining how your application responds to a client request to a particular endpoint. Each route can have one or more handler functions, which are executed when the route is matched.

Here is an example that demonstrates simple routing:

app.get('/', function (req, res) {
res.send('This is the home page');
});

app.get('/about', function (req, res) {
res.send('This is the about page');
});

In this example, the application responds with "This is the home page" when the root of your website (/) is accessed, and with "This is the about page" when the /about route is accessed.

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.

Here is an example of a simple middleware function:

app.use(function (req, res, next) {
console.log('Time:', Date.now());
next();
});

This middleware function simply logs the current time and then passes control to the next middleware function in the stack.

Error Handling

Express comes with a built-in error handler, which takes care of any errors that might occur in your app.

Here is a simple example:

app.use(function (err, req, res, next) {
console.error(err.stack);
res.status(500).send('Something broke!');
});

Serving Static Files

Express provides a built-in middleware function to serve static files. For example, to serve images, CSS files, and JavaScript files in a directory named public, you would use the following code:

app.use(express.static('public'));

Now, you can load the files that are in the public directory:

http://localhost:3000/images/kitten.jpg
http://localhost:3000/css/style.css
http://localhost:3000/js/app.js

This wraps up our introduction to Express.js. We've covered many of the basics, but there is still a lot more to learn. As you continue to explore Express.js, you'll discover that it is a powerful tool for developing web applications.

Next, you should try building a simple application using Express.js. As you build, try using different types of middleware, handle different routes, and serve static files. Remember, the best way to learn is by doing. Happy coding!