Last active
November 25, 2017 16:55
-
-
Save giladno/38934f5b550bd7bbf5fac05bb1e80861 to your computer and use it in GitHub Desktop.
ewrap all
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
| 'use strict'; | |
| const express = require('express'); | |
| const app = (()=>{ | |
| const ewrap = f=>{ | |
| if (f.length>3) | |
| return f; | |
| return (req, res, next)=>(async ()=>f(req, res, next))().catch(next); | |
| }; | |
| let app = express(); | |
| for (let fn of ['use', 'all', 'get', 'post', 'put', 'delete', 'head', 'options']) | |
| { | |
| let orig = app[fn].bind(app); | |
| app[fn] = (...args)=>orig(...args.map(arg=>typeof arg=='function' ? ewrap(arg) : arg)); | |
| } | |
| return app; | |
| })(); | |
| app.all('/test', async (req, res)=>{ | |
| res.send('test'); | |
| }); | |
| app.all('/error', async (req, res)=>{ | |
| throw new Error('error'); | |
| }); | |
| app.use((err, req, res, next)=>{ | |
| console.log(err.stack); | |
| res.status(500).send('Server Error'); | |
| }); | |
| app.listen(8080); | |
(node:23932) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(node:23932) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): Error: error
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this is not working..