将静态内容(Angular 8)与NestJS和Fastify一起使用时的路由问题

问题描述 投票:1回答:1

最近几天,我一直在寻找一种解决方法,以解决将静态内容与NestJS和Fastify一起使用时的路由问题。具体来说,我正在尝试将NestJS托管的Angular 8与Fastify一起使用。我一直在遵循本教程中给出的示例:https://www.djamware.com/post/5d2898430707cc5968d9d57f/build-a-web-app-using-nestjs-fastify-mongodb-and-angular-8

问题是,如果您尝试直接导航到特定URL,例如http://localhost:3000/articles,则Fastify服务器将以包含404错误消息的JSON字符串作为响应。我已经通过克隆教程的GitHub存储库将问题缩小到Fastify特有的问题,并且除了用Express替换Fastify所需的工作外,没有做其他事情。

我正在发布我的代码,希望有人可以告诉我我做错了什么。我想使用Fastify而不是Express,因为如果速度声明是准确的,我真的可以使用提高的性能。我留下了用于切换Express项目的注释掉的代码。感谢您抽出宝贵时间仔细研究甚至尝试提供帮助。

编辑:我刚刚意识到我忘了提供指向GitHub存储库的链接,这里是:https://github.com/didinj/nestjs-fastify-mongodb-angular8.git

main.ts

import { NestFactory } from '@nestjs/core';
import { FastifyAdapter, NestFastifyApplication } from '@nestjs/platform-fastify';
import { AppModule } from './app.module';
import { join } from 'path';


async function bootstrap() {
  // const app = await NestFactory.create(AppModule);

  const app = await NestFactory.create<NestFastifyApplication>(
    AppModule,
    new FastifyAdapter({
      wildcard: false,
      logger: {
        level: 'trace',
        file: '/Users/jcorekin/fastify.log' // Will use pino.destination()
      }
    }),
  );
  app.useStaticAssets({
    root: join(__dirname, '../client/dist/
  });

  await app.listen(3000, '0.0.0.0');
}
bootstrap();

app.module.ts

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ArticleModule } from './article/article.module';
import { ServeStaticModule } from '@nestjs/serve-static';
import { join } from 'path';

@Module({
  imports: 
  [
    // ArticleModule,
    // ServeStaticModule.forRoot({
    //   rootPath: join(__dirname, '../client/dist/client'),
    // }),
  ],
  // controllers: [AppController],
  providers: [AppService],
})
export class AppModule { }

app.service.ts

import { Injectable } from '@nestjs/common';

@Injectable()
export class AppService {
  getHello(): string {
    return 'Hello World!';
  }
}
angular typescript routing nestjs fastify
1个回答
0
投票

我找到了一个解决方案,但很烦人,它需要用Express代替Fastify。看来Fastify在路由方面存在一些问题。

main.ts

import { NestExpressApplication } from '@nestjs/platform-express';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create<NestExpressApplication>(AppModule);
  app.setGlobalPrefix('api');
  await app.listen(3000);
}
bootstrap();

app.module.ts

import { CommentModule } from './comment/comment.module';
import { Module, HttpModule } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ProfessorModule } from './professor/professor.module';
import { ServeStaticModule } from '@nestjs/serve-static';
import { join } from 'path';

@Module({
  imports: [
    ServeStaticModule.forRoot({
      rootPath: join(__dirname, '..', '..', 'client/dist/client'),
    }),
    HttpModule,
    ...
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule { }

我真的很想保留Fastify,但是我今天整整花了整整一天的时间来弄清楚为什么路由中断了,这时我可以简单地更改为express并且效果很好!

[我还将承认,我并没有尝试使用fastify来解决这个确切的解决方案,因此,如果您只保留旧的fastify应用创建代码,请摆脱那个怪异的“ useStaticAssets”调用,而只需使用serve, -static在app.module.ts]

© www.soinside.com 2019 - 2024. All rights reserved.