diff --git a/express/express-tests.ts b/express/express-tests.ts index 45e6b643e..3efdf19a6 100644 --- a/express/express-tests.ts +++ b/express/express-tests.ts @@ -1245,6 +1245,16 @@ if (!module.parent) { ////// +var router = new express.Router(); + +router.get('/', function (req, resp, next?) { + resp.send('response from router'); + resp.end(); + if (next) { + next(); + } +}); + function test_general() { app.use((err, req, res: express.Response) => { @@ -1477,4 +1487,5 @@ function test_middleware() { app.use(express.cookieSession()); app.use(express.directory('public')); app.use(express.static('public')); + app.use(router.middleware); } diff --git a/express/express.d.ts b/express/express.d.ts index 6fa79fc76..b97888998 100644 --- a/express/express.d.ts +++ b/express/express.d.ts @@ -62,6 +62,98 @@ declare module "express" { new (method: string, path: string, callbacks: Function[], options: any): Route; } + interface IRouter { + /** + * Map the given param placeholder `name`(s) to the given callback(s). + * + * Parameter mapping is used to provide pre-conditions to routes + * which use normalized placeholders. For example a _:user_id_ parameter + * could automatically load a user's information from the database without + * any additional code, + * + * The callback uses the samesignature as middleware, the only differencing + * being that the value of the placeholder is passed, in this case the _id_ + * of the user. Once the `next()` function is invoked, just like middleware + * it will continue on to execute the route, or subsequent parameter functions. + * + * app.param('user_id', function(req, res, next, id){ + * User.find(id, function(err, user){ + * if (err) { + * next(err); + * } else if (user) { + * req.user = user; + * next(); + * } else { + * next(new Error('failed to load user')); + * } + * }); + * }); + * + * @param name + * @param fn + */ + param(name: string, fn: Function): T; + + param(name: string[], fn: Function): T; + + /** + * Special-cased "all" method, applying the given route `path`, + * middleware, and callback to _every_ HTTP method. + * + * @param path + * @param fn + */ + all(path: string, fn?: (req: Request, res: Response, next: Function) => any): T; + + all(path: string, ...callbacks: Function[]): void; + + get(name: string, ...handlers: RequestFunction[]): T; + + get(name: RegExp, ...handlers: RequestFunction[]): T; + + post(name: string, ...handlers: RequestFunction[]): T; + + post(name: RegExp, ...handlers: RequestFunction[]): T; + + put(name: string, ...handlers: RequestFunction[]): T; + + put(name: RegExp, ...handlers: RequestFunction[]): T; + + del(name: string, ...handlers: RequestFunction[]): T; + + del(name: RegExp, ...handlers: RequestFunction[]): T; + } + + export class Router implements IRouter { + new (options?: any): Router; + + middleware (): any; + + param(name: string, fn: Function): Router; + + param(name: any[], fn: Function): Router; + + all(path: string, fn?: (req: Request, res: Response, next: Function) => any): Router; + + all(path: string, ...callbacks: Function[]): void; + + get(name: string, ...handlers: RequestFunction[]): Router; + + get(name: RegExp, ...handlers: RequestFunction[]): Router; + + post(name: string, ...handlers: RequestFunction[]): Router; + + post(name: RegExp, ...handlers: RequestFunction[]): Router; + + put(name: string, ...handlers: RequestFunction[]): Router; + + put(name: RegExp, ...handlers: RequestFunction[]): Router; + + del(name: string, ...handlers: RequestFunction[]): Router; + + del(name: RegExp, ...handlers: RequestFunction[]): Router; + } + interface Handler { (req: Request, res: Response, next?: Function): void; } @@ -793,7 +885,7 @@ declare module "express" { (req: Request, res: Response, next: Function): any; } - interface Application { + interface Application extends IRouter { /** * Initialize the server. * @@ -850,35 +942,6 @@ declare module "express" { */ engine(ext: string, fn: Function): Application; - /** - * Map the given param placeholder `name`(s) to the given callback(s). - * - * Parameter mapping is used to provide pre-conditions to routes - * which use normalized placeholders. For example a _:user_id_ parameter - * could automatically load a user's information from the database without - * any additional code, - * - * The callback uses the samesignature as middleware, the only differencing - * being that the value of the placeholder is passed, in this case the _id_ - * of the user. Once the `next()` function is invoked, just like middleware - * it will continue on to execute the route, or subsequent parameter functions. - * - * app.param('user_id', function(req, res, next, id){ - * User.find(id, function(err, user){ - * if (err) { - * next(err); - * } else if (user) { - * req.user = user; - * next(); - * } else { - * next(new Error('failed to load user')); - * } - * }); - * }); - * - * @param name - * @param fn - */ param(name: string, fn: Function): Application; param(name: string[], fn: Function): Application; @@ -1003,16 +1066,6 @@ declare module "express" { configure(fn: Function): Application; - /** - * Special-cased "all" method, applying the given route `path`, - * middleware, and callback to _every_ HTTP method. - * - * @param path - * @param fn - */ - all(path: string, fn?: (req: Request, res: Response, next: Function) => any): Application; - - all(path: string, ...callbacks: Function[]): void; /** * Render the given view `name` name with `options` @@ -1033,21 +1086,6 @@ declare module "express" { render(name: string, callback: (err: Error, html: string) => void): void; - get(name: string, ...handlers: RequestFunction[]): any; - - get(name: RegExp, ...handlers: RequestFunction[]): any; - - post(name: string, ...handlers: RequestFunction[]): any; - - post(name: RegExp, ...handlers: RequestFunction[]): any; - - put(name: string, ...handlers: RequestFunction[]): any; - - put(name: RegExp, ...handlers: RequestFunction[]): any; - - del(name: string, ...handlers: RequestFunction[]): any; - - del(name: RegExp, ...handlers: RequestFunction[]): any; /** * Listen for connections. @@ -1759,7 +1797,9 @@ declare module "express" { function urlencoded(): any; function multipart(): any; + } export = e; } +