Skip to content

Instantly share code, notes, and snippets.

@albinekb
Last active September 12, 2018 13:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save albinekb/59ded3fd053b55a5b01228e819a33283 to your computer and use it in GitHub Desktop.
Save albinekb/59ded3fd053b55a5b01228e819a33283 to your computer and use it in GitHub Desktop.
express async route wrapper
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