fastify引擎

86 阅读1分钟

fastify

介绍

nest默认express
集成 Fastify 可显著提升性能

使用

npm install @nestjs/platform-fastify fastify
main.ts

async function bootstrap() {
  // const app = await NestFactory.create(AppModule);
  const app = await NestFactory.create<NestFastifyApplication>(
    AppModule,
    //  new FastifyAdapter({ logger: true }) // 启用 Fastify 日志
    new FastifyAdapter(),
    // {logger:['error','warn','log']}
  );

区别

const response = ctx.getResponse<FastifyReply>();
const request = ctx.getRequest<FastifyRequest>();

静态资源

@fastify/static
// 注册静态文件插件
await app.register(require('@fastify/static'), {
    root: path.join(process.cwd(), 'public'),
    prefix: '/static/',
});
文件放在public下,请求时加static前缀
http://localhost:5000/static/image/defaultUserAvatar.png

待探索

HTTPS 支持
Schema 验证
生命周期钩子(Hooks
速率限制
@fastify/rate-limit
JWT 认证优化
@fastify/jwt
连接池优化
fastify-postgres
流式响应处理
集群模式启动