【问题标题】:How i can integrate next with nest?我如何才能将下一个与嵌套集成?
【发布时间】:2020-06-22 10:14:26
【问题描述】:

我尝试在自定义适配器函数的渲染中包含next

// custom.adapter.ts
import { ExpressAdapter } from '@nestjs/platform-express';
import { Response } from 'express';
import next  from 'next';

const dev = true;
const nextApp = next({ dev });
const handle = nextApp.getRequestHandler();

export class NextAdapter extends ExpressAdapter {
    constructor() { super() }
    
    render(res: Response, view: string, options: any) {
        const { req } = options;
        return new Promise<void>(async (resolve) => {
            nextApp.prepare().then(async () => {

                if (req.query = '/a') {
                    await nextApp.render(req, res, '/a', req.query)
                } else {
                    handle(req, res);
                }
                resolve();
            });
        });
    }
}

但这不起作用,我尝试获取该页面并收到很多 404 错误。

// main.ts
import { NestFactory } from '@nestjs/core';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
import { AppModule } from './app.module';
import { NextAdapter } from './next.adapter';



async function bootstrap() {
  const app = await NestFactory.create(AppModule, new NextAdapter());

  const options = new DocumentBuilder()
    .setTitle('Blog Rest API dosc')
    .build();
  const document = SwaggerModule.createDocument(app, options);
  SwaggerModule.setup('api', app, document);

  await app.listen(3000);
}

bootstrap();

我在github上发布的所有代码 https://github.com/MiiZZo/nest-next/tree/master/nextapp
如果有人帮助我,我将不胜感激,谢谢!

【问题讨论】:

    标签: nestjs next


    【解决方案1】:

    一种解决方案是使用nest-nexthttps://www.npmjs.com/package/nest-next 详细文档在他们的页面上,但这里是相关代码,用于在嵌套 js 中包含下一个应用程序渲染:

    import { Module } from '@nestjs/common';
    import Next from 'next';
    import { RenderModule } from 'nest-next';
    
    @Module({
      imports: [
        RenderModule.forRootAsync(Next({ dev: process.env.NODE_ENV !== 'production' })),
        ...
      ],
      ...
    })
    export class AppModule {}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-04
      • 1970-01-01
      • 2021-04-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多