npm i koa2-event-router -S
// index.js
const Koa = require('koa');
const eventRouter = require('koa2-event-router');
const app = new Koa();
app.use(eventRouter({
path: `${__dirname}/src`,
filename: /\.controller\.js$/,
}));
app.listen(9999);
// __dirname/src/**/**.controller.js
router.on('get /api/v2/test', async (ctx) => {
ctx.body = 'Hello World!';
});
// open http://localhost:9999/api/v2/test
Name |
Type |
Require |
Default |
Description |
path |
String |
true |
null |
scanning entrance |
filename |
RegExp |
false |
/\.(js|ts)$/ |
controller file matching rule |