import Koa from '@eggjs/koa';
import Router from '@eggjs/router';
const app = new Koa();
const router = new Router();
router.get('/', async (ctx, next) => {
// ctx.router available
});
app
.use(router.routes())
.use(router.allowedMethods());
router.get|put|post|patch|delete|del ⇒ Router
Create router.verb() methods, where verb is one of the HTTP verbs such
as router.get() or router.post().
Match URL patterns to callback functions or controller actions using router.verb(),
where verb is one of the HTTP verbs such as router.get() or router.post().
Additionaly, router.all() can be used to match against all methods.
Middleware run in the order they are defined by .use(). They are invoked
sequentially, requests start at the first middleware and work their way
“down” the middleware stack.
// session middleware will run before authorize
router
.use(session())
.use(authorize());
// use middleware only with given path
router.use('/users', userAuth());
// or with an array of paths
router.use(['/users', '/admin'], userAuth());
app.use(router.routes());
router.prefix(prefix) ⇒ Router
Set the path prefix for a Router instance that was already initialized.
Returns separate middleware for responding to OPTIONS requests with
an Allow header containing the allowed methods, as well as responding
with 405 Method Not Allowed and 501 Not Implemented as appropriate.
throw the returned value in place of the default NotImplemented error
[options.methodNotAllowed]
function
throw the returned value in place of the default MethodNotAllowed error
Example
import Koa from '@eggjs/koa';
import Router from '@eggjs/router';
const app = new Koa();
const router = new Router();
app.use(router.routes());
app.use(router.allowedMethods());
@eggjs/router
Router core component for Egg.js.
API Reference
Router ⏏
Kind: Exported class
new Router([opts])
Create a new router.
ObjectStringExample Basic usage:
router.get|put|post|patch|delete|del ⇒
RouterCreate
router.verb()methods, where verb is one of the HTTP verbs such asrouter.get()orrouter.post().Match URL patterns to callback functions or controller actions using
router.verb(), where verb is one of the HTTP verbs such asrouter.get()orrouter.post().Additionaly,
router.all()can be used to match against all methods.When a route is matched, its path is available at
ctx.routePathand if named, the name is available atctx.routeNameRoute paths will be translated to regular expressions using path-to-regexp.
Query strings will not be considered when matching requests.
Named routes
Routes can optionally have names. This allows generation of URLs and easy renaming of URLs during development.
Multiple middleware
Multiple middleware may be given:
Nested routers
Nesting routers is supported:
Router prefixes
Route paths can be prefixed at the router level:
URL parameters
Named route parameters are captured and added to
ctx.params.The path-to-regexp module is used to convert paths to regular expressions.
Kind: instance property of
RouterStringfunctionfunctionrouter.routes ⇒
functionReturns router middleware which dispatches a route matching the request.
Kind: instance property of
Routerrouter.use([path], middleware) ⇒
RouterUse given middleware.
Middleware run in the order they are defined by
.use(). They are invoked sequentially, requests start at the first middleware and work their way “down” the middleware stack.Kind: instance method of
RouterStringfunctionfunctionExample
router.prefix(prefix) ⇒
RouterSet the path prefix for a Router instance that was already initialized.
Kind: instance method of
RouterStringExample
router.allowedMethods([options]) ⇒
functionReturns separate middleware for responding to
OPTIONSrequests with anAllowheader containing the allowed methods, as well as responding with405 Method Not Allowedand501 Not Implementedas appropriate.Kind: instance method of
RouterObjectBooleanfunctionfunctionExample
Example with Boom
router.redirect(source, destination, [code]) ⇒
RouterRedirect
sourcetodestinationURL with optional 30x statuscode.Both
sourceanddestinationcan be route names.This is equivalent to:
Kind: instance method of
RouterStringStringNumberrouter.route(name) ⇒
Layer|falseLookup route with given
name.Kind: instance method of
RouterStringrouter.url(name, params, [options]) ⇒
String|ErrorGenerate URL for route. Takes a route name and map of named
params.Kind: instance method of
RouterStringObjectObjectObject|StringExample
router.param(param, middleware) ⇒
RouterRun middleware for named route parameters. Useful for auto-loading or validation.
Kind: instance method of
RouterStringfunctionExample
Router.url(path, params [, options]) ⇒
StringGenerate URL from url pattern and given
params.Kind: static method of
RouterStringObjectObjectObject|StringExample
Tests
Run tests using
npm test.Breaking changes on v3
License
MIT
Contributors
Made with contributors-img.