Skip to content

Instantly share code, notes, and snippets.

@kirill3333
Last active June 15, 2017 15:35
Show Gist options
  • Save kirill3333/2e65af1ee505f6ddb2326f44a873dbc2 to your computer and use it in GitHub Desktop.
Save kirill3333/2e65af1ee505f6ddb2326f44a873dbc2 to your computer and use it in GitHub Desktop.
Example of express middleware
const cache = require('./cache')
const db = require('./db')
function sessionMiddleware(req, res, next) {
const userId = req.query.id
const user = cache.get(userId)
if (!user) {
db.findUserById(userId).then((result) => {
res.body = result
next()
})
} else {
res.body = user
next()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment