简体   繁体   English

如何将分隔的Koa-Router与Typescript结合

[英]How to combine separated Koa-Routers with Typescript

I decided to split my routers by their purpose, so they look like this: 我决定按用途划分路由器,因此它们如下所示:

routers/homeRouter.ts 路由器/ homeRouter.ts

import * as Router from 'koa-router';

const router: Router = new Router();

router
    .get('/', async (ctx, next) => {
        ctx.body = 'hello world';
    });

export = router;

routers/userRouter.ts 路由器/ userRouter.ts

import * as Router from 'koa-router';
import UserController = require('../controller/userController');

const router: Router = new Router(
    {
        prefix: 'users'
    }
);

var userController = new UserController();

router
    .post('/user/:email/:password', userController.signUp);

export = router;

With this, my app.ts has to import each of the routers one by one like this: 这样,我的app.ts必须像这样一个接一个地导入每个路由器:

app.ts app.ts

import * as Koa from 'koa';
import * as homeRouter from './routers/homeRouter';
import * as userRouter from './routers/userRouter';
const app: Koa = new Koa();
app
    .use(homeRouter.routes())
    .use(homeRouter.allowedMethods());
app
    .use(userRouter.routes())
    .use(userRouter.allowedMethods());
app.listen(3000);

But what I want is this: 但是我想要的是:

app.ts app.ts

import * as Koa from 'koa';
import * as routers from './routers';
const app: Koa = new Koa();
app
    .use(routers.routes())
    .use(routers.allowedMethods());
app.listen(3000);

I do not know how to export the routers to achieve this. 我不知道如何导出路由器来实现这一目标。 Can anyone help? 有人可以帮忙吗?

You can have something like this: 你可以有这样的事情:

userRouter.ts userRouter.ts

import * as Router from 'koa-router';

const router = new Router();
router.get('/', list);

...

export default router.routes();

routes.ts routes.ts

import * as Router from 'koa-router';


import UserRouter from './userRouter';
import HomeRouter from './homeRouter';

const apiRouter = new Router({ prefix: '/api'});
apiRouter.use('/users', UserRouter);
apiRouter.use('/home', HomeRouter);

export default apiRouter.routes();

You can implement each router separately as you have done, then generate a new router which includes all of your routers. 您可以分别实现每个路由器,然后生成一个包含所有路由器的新路由器。 Then you can include it in your app.ts file. 然后,您可以将其包含在app.ts文件中。

Meseret is a nice typescript library I wrote that can help you manage your Koa server routers, middleware and much more. Meseret是我写的一个不错的打字稿库,可以帮助您管理Koa服务器路由器,中间件等等。

Eg You can replace your app.ts with this. 例如,您可以用此替换您的app.ts

import { ServerApp } from 'meseret'

import HomeRouter from './routers/homeRouter'
import UserRouter from './routers/userRouter'

new ServerApp({
  name: 'Your App Name',
  httpServers: [
    { port: 3000 }
  ],
  routers: [
    HomeRouter,
    UserRouter
  ]
}).start() // returns a Promise

All you have to do is just import your koa-router Routers and add them to the list of routers in the ServerApp . 您所要做的只是导入koa-router路由器,并将它们添加到ServerApp中的routers列表中。

Meseret helps you do much more than manage routers. Meseret不仅可以管理路由器,还能帮助您完成更多工作。 It has built in support for Koa sessions, static caching, public directory serving, response compression and more. 它内置了对Koa会话,静态缓存,公共目录服务,响应压缩等的支持。 There's also, as expected, an option that lets you add your own Koa middleware to the app. 正如预期的那样,还有一个选项可让您将自己的Koa中间件添加到应用程序。

Plus, it can connect to MongoDB and help manage your Mongoose models (even by adding static type support to your mongoose models--the paths, methods and statics--if you use the ModelFactory ). 另外,它可以连接到MongoDB并帮助管理您的Mongoose模型(即使通过使用ModelFactory ,也可以向您的Mongoose模型添加静态类型支持(路径,方法和静态ModelFactory ))。

Socket.IO web sockets are also supported in the config. 配置中还支持Socket.IO Web套接字。

All these features are managed in a single ServerApp instance. 所有这些功能都在单个ServerApp实例中进行管理。

Find out more at meseret's Github page . meseret的Github页面上找到更多信息。

I hope it helps you manage your distributed node server project. 我希望它可以帮助您管理分布式节点服务器项目。

I made a utility awhile back for combining multiple instances of koa-router into a single middleware, and I just now added a TypeScript definition file, so it should be usable for TS folks. 不久前制作了一个实用程序,用于将koa-router多个实例组合到一个中间件中,并且我现在刚刚添加了TypeScript定义文件,因此它对TS人员应该可用。 Just npm install koa-combine-routers and use it like so: 只需npm install koa-combine-routers并像这样使用它:

Usage: 用法:

app.js app.js

import * as Koa from 'koa'
import router from './router'

const app = new Koa()

app.use(router())

routes.js routes.js

import * as Router from 'koa-router'
import * as combineRouters from 'koa-combine-routers'

const dogRouter = new Router()
const catRouter = new Router()

dogRouter.get('/dogs', async ctx => {
  ctx.body = 'ok'
})

catRouter.get('/cats', async ctx => {
  ctx.body = 'ok'
})

const router = combineRouters(
  dogRouter, 
  catRouter
)

export default router

This will use both .routes() and .allowedMethods() . 这将同时使用.routes().allowedMethods()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM