basic middleware concepts in node



~2 min read


347 words

Middleware is software that sits between two other pieces of software and can augment, change, modify, or even respond to data it receives.

Normally, its purpose is to to receive data, add or modify it, and forward it along to the next piece of software that can determine how to handle it.

Let’s look at a basic node server and look at how we can import middleware and write our own:

// import a server framework
import express from ‘express’
// import middleware
import { json, urlencoded } from ‘body-parser’
import Morgan from ‘Morgan’
import cors from ‘cors’

// define our port
const port = process.env.PORT || 3000

// create our server
export const app = express()


// use imported middleware
app.use(urlencoded({ extended: true }))

// define custom middleware
const log = (req, res, next) => {
  next() // next _with_ an argument is treated like an error

// apply a custom middleware solution (`log`) _before_ the callback controller on this route
app.get('/data', log, (req, res) => {
  res.send({ data: [1, 2, 3, 4] })
})’, [log, log], (req, res) => {

export const start = () => {
  app.listen(port, () => {
    console.log(`server running @${port}`)

In the above example, we only apply our custom middleware log to the .get verb on the /data route and log twice on the .post by passing in the middleware in an array. Another option would have been to add the middleware to every route, however, by using the app.use just like we did for the cors, json, etc. There are many other approaches. These are just some of the basics.

The important thing to remember is that controller functions (the functions passed into express’s app dot verb methods) are defined with three arguments (though the third is used primarily for middleware): req for request, res for response, and next. If next is defined, the controller looks for the next function to run. If it’s not, then the controller terminates the process and listens for another request.

Hi there and thanks for reading! My name's Stephen. I live in Chicago with my wife, Kate, and dog, Finn. Want more? See about and get in touch!