Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
@jfkz/nest-koa-adapter
Advanced tools
Readme
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.
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));
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('*');
}
}
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 inorigin
property which should not be function.
const app = NestFactory.create<NestKoaApplication>(AppModule, new KoaAdapter());
app.enableCors({});
await app.init();
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 fromkoa-static
.
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();
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
.
@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);
}
}
FAQs
Koa HTTP adapter for Nest.js
The npm package @jfkz/nest-koa-adapter receives a total of 0 weekly downloads. As such, @jfkz/nest-koa-adapter popularity was classified as not popular.
We found that @jfkz/nest-koa-adapter demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.