Middleware explained with a basic NodeJs & Express authentication.

Javascript — a love.
const authenticate = (req, res, next) => {
// parse the user - or anything else -
// out of the request body.
let user = req.body.user
// do some checking
if (user === database.user) {
// user successfully authenticated -> call next, which
// terminates the middleware-function and the parent
// continues.
next()
)
// user didn't match any we know? Respond with authentication
// failure.
res.status(401);
}
// apply it to all routes
app.use(authenticate)
// apply it to a certain route
app.get('/sensibledata', authenticate, (req, res) => {
// only authenticated user will have access to the
// following code - everyone else received a status
// 401 to their request.
res.send({ data: sensibleData })
}

--

--

Works at Bosch.IO as a Software Engineer. Thrilled about coding, beer and other technical stuff ;).

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
David Joos

David Joos

Works at Bosch.IO as a Software Engineer. Thrilled about coding, beer and other technical stuff ;).