diff --git a/express/express-tests.ts b/express/express-tests.ts index de39e2c8a..6ac53bf2d 100644 --- a/express/express-tests.ts +++ b/express/express-tests.ts @@ -17,6 +17,12 @@ app.use(function(req, res, next){ next(); }); +app.use(function(err: any, req: express.Request, res: express.Response, next: express.NextFunction) { + console.error(err); + next(err); +}); + + app.get('/', function(req, res){ res.send('hello world'); }); @@ -47,6 +53,13 @@ router.route('/users') res.send(req.query['token']); }); +router.get('/user/:id', function(req, res, next) { + if (req.params.id == 0) next('route'); + else next(); +}, function(req, res, next) { + res.render('regular'); +}); + app.use((req, res, next) => { // hacky trick, router is just a handler router(req, res, next); diff --git a/express/express.d.ts b/express/express.d.ts index 65848860b..171b6e6a7 100644 --- a/express/express.d.ts +++ b/express/express.d.ts @@ -407,9 +407,9 @@ declare module "express" { originalUrl: string; url: string; - + baseUrl: string; - + app: Application; } @@ -796,18 +796,23 @@ declare module "express" { charset: string; } + interface NextFunction { + (): void; + (err: any): void; + } + interface ErrorRequestHandler { - (err: any, req: Request, res: Response, next: Function): any; + (err: any, req: Request, res: Response, next: NextFunction): any; } interface RequestHandler { - (req: Request, res: Response, next: Function): any; + (req: Request, res: Response, next: NextFunction): any; } interface Handler extends RequestHandler {} interface RequestParamHandler { - (req: Request, res: Response, next: Function, param: any): any; + (req: Request, res: Response, next: NextFunction, param: any): any; } interface Application extends IRouter, Express.Application {