Merge pull request #1444 from valotas/express-router

Express.Router definition
This commit is contained in:
Masahiro Wakame
2013-12-26 19:33:42 -08:00
2 changed files with 106 additions and 55 deletions

View File

@@ -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);
}

150
express/express.d.ts vendored
View File

@@ -62,6 +62,98 @@ declare module "express" {
new (method: string, path: string, callbacks: Function[], options: any): Route;
}
interface IRouter<T> {
/**
* 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<Router> {
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<Application> {
/**
* 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;
}