Last active
June 15, 2017 15:35
-
-
Save kirill3333/2e65af1ee505f6ddb2326f44a873dbc2 to your computer and use it in GitHub Desktop.
Example of express middleware
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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