package
koa supercharged. routing. middleware library. ready-to-go web-application-server
npm install koa-magicGitHub-flavored package documentation.
Across all versions.
Download trend unavailable.
Runtime metadata.
Tagged topics.
koa supercharged. middleware library. ready-to-go web-application-server based on koajs v2
$ npm install koa-magic --save
$ yarn add koa-magickoa-magic provides the following modules
const {Koa, Router, ApplicationServer, ErrorLogger, Dispatcher} = require('koa-magic');Basic Koa Class extended by routing methods.
Example 1 - path routing
const Router = require('koa-magic').Router;
const Koa = require('koa-magic').Koa;
// initialize koa + router
const webapp = new Koa();
const myrouter = new Router();
// attach handler to root path
koa.get('/helloworld.html', ctx => {
ctx.body = 'Helloworld';
});
// attach router middleware - use attach() of extended Koa!
koa.attach('/hello', myrouter);
// start server
koa.listen(...);The mount-path is processed by path-to-regexp to enable parametric path expressions. The parameters are exposed by the ctx.params object.
Note: use .attach() instead of .use() to add middleware to the stack. It handles flat middleware functions like (ctx, next) => {} as well as instances of Router!
The following methods are provided by each routing instance (or extended Koa class)
HTTP
get(path:string|RegExp, middleware:Promise) handle http-getpost(path:string|RegExp, middleware:Promise) handle http-postput(path:string|RegExp, middleware:Promise) handle http-puthead(path:string|RegExp, middleware:Promise) handle http-headdelete(path:string|RegExp, middleware:Promise) handle http-deleteoptions(path:string|RegExp, middleware:Promise) handle http-optionsall(path:string|RegExp, middleware:Promise) handle all kind of http-methodsVHOST
vhost(hostname:string|RegExp, middleware:Promise|Router) virtual host routingROUTING
attach(path:string|RegExp, middleware:Promise|Router) add a new router to the current stack (like use)Example 1 - path routing
const Router = require('koa-magic').Router;
const Koa = require('koa-magic').Koa;
// initialize koa + router
const webapp = new Koa();
const myrouter = new Router();
myrouter.get('/world.html', ctx => {
ctx.body = "ook";
});
myrouter.post('/:action/:view?', ctx => {
ctx.body = "action:" + ctx.params.action + ' - ' + ctx.params.view;
});
Example 2 - path routing with request forwarding
const Router = require('koa-magic').Router;
const Koa = require('koa-magic').Koa;
// initialize koa + router
const webapp = new Koa();
const myrouter = new Router();
myrouter.get('(.*)', (ctx, next) => {
dosomething();
return next();
});
myrouter.post('/:action/:view?', ctx => {
ctx.body = "action:" + ctx.params.action + ' - ' + ctx.params.view;
Example 3 - vhost routing
const Router = require('koa-magic').Router;
const Koa = require('koa-magic').Koa;
// initialize koa + router
const webapp = new Koa();
const myrouter = new Router();
myrouter.get('/world.html', ctx => {
ctx.body = "ook";
});
// attach router middleware to domain
koa.vhost('example.org', myrouter);
// start server
koa.listen(...);koa-magic is OpenSource and licensed under the Terms of The MIT License (X11) - your're welcome to contribute
Estimated unpacked size across 54 packages.
8.0 MB
Includes dependencies.
12 direct · 4 outdated
^3.1.5
7.0.2
^2.13.1
3.3.0
^1.4.0
1.4.0
^1.1.0
1.1.0
^4.1.0
4.1.0
^6.2.0
9.0.0
^3.1.0
3.1.0
^2.0.1
2.0.1
^2.1.0
2.1.0