TopGit theo dõi ZijianHe/koa-router trên GitHub, đã đạt 4.8k sao. Router middleware for koa.
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
VÌ SAO CHƯA CÓ REVIEW
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
Express-style routing using app.get, app.put, app.post, etc.
Named URL parameters.
Named routes with URL generation.
Responds to OPTIONS requests with allowed methods.
Support for 405 Method Not Allowed and 501 Not Implemented.
Multiple route middleware.
Multiple routers.
Nestable routers.
ES7 async/await support.
Migrating to 7 / Koa 2
The API has changed to match the new promise-based middleware
signature of koa 2. See the
koa 2.x readme for more
information.
Middleware is now always run in the order declared by .use() (or .get(),
etc.), which matches Express 4 API.
Installation
Install using npm:
npm install koa-router
API Reference
koa-router
Router ⏏
new Router([opts])
instance
.get|put|post|patch|delete|del ⇒ Router
.routes ⇒ function
.use([path], middleware) ⇒ Router
.prefix(prefix) ⇒ Router
.allowedMethods([options]) ⇒ function
.redirect(source, destination, [code]) ⇒ Router
.route(name) ⇒ Layer | false
.url(name, params, [options]) ⇒ String | Error
.param(param, middleware) ⇒ Router
static
.url(path, params) ⇒ String
Router ⏏
Kind: Exported class
new Router([opts])
Create a new router.
Param
Type
Description
[opts]
Object
[opts.prefix]
String
prefix router paths
Example
Basic usage:
var Koa = require('koa');
var Router = require('koa-router');
var app = new Koa();
var router = new Router();
router.get('/', (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.
var forums = new Router();
var posts = new Router();
posts.get('/', (ctx, next) => {...});
posts.get('/:pid', (ctx, next) => {...});
forums.use('/forums/:fid/posts', posts.routes(), posts.allowedMethods());
// responds to "/forums/123/posts" and "/forums/123/posts/123"
app.use(forums.routes());
Router prefixes
Route paths can be prefixed at the router level:
var router = new Router({
prefix: '/users'
});
router.get('/', ...); // responds to "/users"
router.get('/:id', ...); // responds to "/users/:id"
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 Router
Param
Type
Description
path
String
[middleware]
function
route middleware(s)
callback
function
route callback
router.routes ⇒ function
Returns router middleware which dispatches a route matching the request.
Kind: instance property of Router
router.use([path], middleware) ⇒ Router
Use 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 Router
Param
Type
[path]
String
middleware
function
[...]
function
Example
// 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.
Kind: instance method of Router
Param
Type
prefix
String
Example
router.prefix('/things/:thing_id')
router.allowedMethods([options]) ⇒ function
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.
Kind: instance method of Router
Param
Type
Description
[options]
Object
[options.throw]
Boolean
throw error instead of setting status and header
[options.notImplemented]
function
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
var Koa = require('koa');
var Router = require('koa-router');
var app = new Koa();
var router = new Router();
app.use(router.routes());
app.use(router.allowedMethods());
Example with Boom
var Koa = require('koa');
var Router = require('koa-router');
var Boom = require('boom');
var app = new Koa();
var router = new Router();
app.use(router.routes());
app.use(router.allowedMethods({
throw: true,
notImplemented: () => new Boom.notImplemented(),
methodNotAllowed: () => new Boom.methodNotAllowed()
}));
Trang TopGit này là một snapshot — tab "Readme" hiển thị nguyên văn README của repo (đã bỏ link, giữ ảnh). Repo GitHub ở github.com/ZijianHe/koa-router là nguồn chính thức.
ZijianHe/koa-router có bao nhiêu sao?
ZijianHe/koa-router có 4.8k sao GitHub — tải lại trang để xem số mới nhất, hoặc xem trực tiếp github.com/ZijianHe/koa-router. TopGit phản chiếu số sao của GitHub nhưng không cam kết đến từng phút.
ZijianHe/koa-router có phải mã nguồn mở không?
Có — ZijianHe/koa-router phát hành theo license MIT, nghĩa là mã nguồn mở để đọc, fork và (tùy license) tái sử dụng. Mã: github.com/ZijianHe/koa-router.
ZijianHe/koa-router có website riêng không?
TopGit chưa ghi nhận URL trang chủ cho ZijianHe/koa-router. Phần README ở tab phía trên thường có link demo, hoặc xem mô tả GitHub của repo.
ZijianHe/koa-router là gì?
ZijianHe/koa-router (ZijianHe/koa-router) là dự án JavaScript trên GitHub. Theo mô tả gốc: Router middleware for koa.
Đọc đầy đủ README ở tab phía trên.
Vẫn đang phân vân về koa-router?
Một cú bấm sẽ gửi câu hỏi kèm trang này cho AI — xem AI nói gì về koa-router.