• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

seidelmartin/nest-koa-adapter: Koa Http adapter for Nest.js

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

seidelmartin/nest-koa-adapter

开源软件地址:

https://github.com/seidelmartin/nest-koa-adapter

开源编程语言:

TypeScript 99.1%

开源软件介绍:

Nest Koa HTTP Adapter

This package allows to use Koa and Koa router together with Nest.js framework.

It consists of KoaAdapter which is basically just mapping between Nest server and Koa. NestKoaApplication is an interface of application created by NestFactory. NestKoaMiddleware and NestKoaFunctionalMiddleware are interfaces for writing middleware for Nest together with Koa. koaToNestMiddleware is an utility function which can convert your old Koa middleware so it can be used in Nest.

How to use

Create application

NestFactory.create<NestKoaApplication>(AppModule, new KoaAdapter());

// You can also pass your own instance of Koa app to the adapter
const koa = new Koa();
NestFactory.create<NestKoaApplication>(AppModule, new KoaAdapter(koa));

Middleware

You can still use your old middleware by converting by using koaToNestMiddleware function.

const koaMiddleware = (ctx, next) => {
  ...
}

@Module({
  controllers: [HelloWorldController],
})
class TestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer
      .apply(koaToNestMiddleware(koaMiddleware))
      .forRoutes('*');
  }
}

Or you can implement class middleware by implementing NestKoaMiddleware interface.

class Middleware implements NestKoaMiddleware {
  use(req: Koa.Request, res: Koa.Response, next: Koa.Next) {
    ...
  }
}

@Module({
  controllers: [HelloWorldController],
})
class TestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer
      .apply(Middleware)
      .forRoutes('*');
  }
}

CORS

To use CORS you'll have to install @koa/cors package.

npm install @koa/cors

After installation is done you can set CORS same way as in normal NEST application.

The enableCors method accepts options same as normal Nest application. The only difference is in origin property which should not be function.

const app = NestFactory.create<NestKoaApplication>(AppModule, new KoaAdapter());

app.enableCors({});

await app.init();

Static assets

To use static assets you have to install koa-static package.

npm install koa-static

Once you have the dependency installed you can set you static assets folder.

const app = NestFactory.create<NestKoaApplication>(AppModule, new KoaAdapter());

app.useStaticAssets(path.join(__dirname, 'static'));

// Or with options
app.useStaticAssets(path.join(__dirname, 'static'), options);

await app.init();

The useStaticAssets method also accepts options which are exactly same as those from koa-static.

Views engine

To use MVC pattern you'll have to install koa-views package. This package allows you to use your favourite templating system.

npm install koa-views

To setup the view engine you have to specify your views folder and other options from koa-views or consolidate which is used under the hood.

const app = NestFactory.create<NestKoaApplication>(AppModule, new KoaAdapter());

app.setViewEngine({
  viewsDir: path.join(__dirname, 'views'),
  map: {
    html: 'lodash',
  },
});

await app.init();

Caveats

Nest components which operates with with Koa response like exception filters needs to use the koaReply utility function from this package because the implementation if the reply in adapter doesn't allow to use standard way of setting body and status properties.

Another option is to inject the HttpAdapterHost dependency and use reply reply method from httpAdapter object. But this is basically the same as using the koaReply.

Example

@Catch()
export class ErrorFilter implements ExceptionFilter {
  public catch(error: any, host: ArgumentsHost): void {
    const httpArguments = host.switchToHttp().getResponse();

    // Your exception handling logic

    const reply = {};
    const statusCode = 500;

    koaReply(httpArguments.getResponse(), reply, statusCode);
  }
}



鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap