npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nest-koa-adapter

v3.1.0

Published

Koa HTTP adapter for Nest.js

Downloads

693

Readme

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

Versioning feature is not yet supported.

Nest components which operates 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);
  }
}