NodeJS: How To Add Additional Middleware To Our Express Server
Tags: beginners, tutorial, javascript, node
Published: 2019-09-03 15:39:12

---
title: NodeJS: How To Add Additional Middleware To Our Express Server
published: true
description: NodeJS: How To Add Additional Middleware To Our Express Server
tags: beginners, tutorial, javascript, node
series: Intro to NodeJS
---

## Intro

So we [installed NodeJS](https://dev.to/miku86/what-is-nodejs-how-to-install-it-1900) on our machine.

We also learned [how to create a simple server using express](https://dev.to/miku86/nodejs-how-to-create-a-simple-server-using-express-1n9d).

Now we want to learn how to add additional middleware to our express server.

## Reuse our simple server from the [last article](https://dev.to/miku86/nodejs-how-to-create-a-simple-server-using-express-1n9d)

- Open your terminal
- Create a file named `index.js`:

```sh
touch index.js
```

- Add this JavaScript code into it:

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

const PORT = 8080;

app.get('/', (request, response) => {
  response.send('Hello World');
});

app.listen(PORT, () => {
  console.log(`Server running at: http://localhost:${PORT}/`);
});
```

**Note**: This is our starting point, we have one working route.
I updated `req` to `request` & `res` to `response` to increase readability.

---

## Additional Middleware

What is Middleware?

Sometimes you want to add additional functionality between the default request-response-cycle. Let's say you want to get some detailed information about the current request.

You can write a simple middleware, that handles this task, and add it to your express app.


## Create a Middleware

- Create a file named `logger.js`:

```sh
touch logger.js
```

- Add this JavaScript code into it:

```js
const logger = (req, res, next) => {
  console.log(`Time: ${new Date()} - Method: ${req.method} - Path: ${req.originalUrl}`);
  next();
};

module.exports = logger;
```

- Require the new logger in index.js and use the middleware:

```js
// import express (after npm install express)
const express = require('express');

// import the logger
const logger = require('./logger');

// create new express app and save it as app
const app = express();

// server configuration
const PORT = 8080;

// use the middleware
app.use(logger);

// create a route for the app
app.get('/', (req, res) => {
  res.send('Hello World');
});

// make the server listen to requests
app.listen(PORT, () => {
  console.log(`Server running at: http://localhost:${PORT}/`);
});
```


## Run it from the terminal

- Run it: 

```sh
node index.js
```

- Visit: [http://localhost:8080](http://localhost:8080)

- Console Result: 

```sh
Server running at: http://localhost:8080/
Time: Tue Sep 03 2019 17:32:30 GMT+0200 (Central European Summer Time) - Method: GET - Path: /
```

---

## Series on Express and PostgreSQL

Here you can find [my series about Express and PostgreSQL](https://dev.to/miku86/nodejs-postgresql-how-to-connect-our-database-to-our-simple-express-server-without-an-orm-10o0)

---

## Further Reading

- [express](https://www.npmjs.com/package/express)
- [Using Middleware](http://expressjs.com/en/guide/using-middleware.html)
- [app.use()](https://expressjs.com/en/4x/api.html#app.use)
- [Middleware callback function examples](https://expressjs.com/en/4x/api.html#middleware-callback-function-examples)


---

## Questions

- What's your favorite middleware for express?