Middleware Functions

Middleware functions are called in the order taken into use with the express server use method. This is especially true when our middleware needs to handle token extraction first in order to add to request object for our route event handlers to use.

app.use(middleware.tokenExtractor)
app.use('/api/blogs', blogsRouter)
app.use('/api/users', usersRouter)
app.use('/api/login', loginRouter)

Normal middleware is a function with three parameters. Last parameter next moves control to next middleware function.

const tokenExtractor = (request, response, next) => {
const token = getTokenFrom(request)
request.token = token
next()
}