Last active
September 12, 2018 13:33
-
-
Save albinekb/59ded3fd053b55a5b01228e819a33283 to your computer and use it in GitHub Desktop.
express async route wrapper
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
type Method = 'get' | 'post' | 'put' | 'delete' | |
type Handler = (req: Request) => Promise<any> | |
function route (method: Method, path: string, handler: Handler) { | |
app[method](path, (req, res, next) => { | |
handler(req) | |
.then(result => result ? res.json(result) : res.status(204).end('')) | |
.catch(next) | |
}) | |
} | |
route('post', '/test', async () => {}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment