小编Jus*_*man的帖子

如何通过部署在 Heroku 上的 Nodejs/Nestjs 服务器为我的 Angular 前端提供服务?

我有一个来自 Angular 的 NestJS api,我想用它来为前端提供服务。这已部署到 heroku 上。通过后端路由使用前缀“/api”,我希望所有其他请求都使用我构建的 Angular 应用程序中的 index.html 进行响应。

我部署的应用程序的文件夹结构是:

-dist
  -client
    -index.html
    -.js files
  -server
    -main.js
    -other dirs/modules
Run Code Online (Sandbox Code Playgroud)

到目前为止,我已经尝试过各种方法,并且它们都在本地工作。问题是部署到heroku。我得到的最接近的(在本文的帮助下:https://medium.com/@bo.vandersteene/use-nest-as-your-server-side-application-with-an-angular-frontend-540b0365bfa3)正在 NestJS 应用程序中创建一个前端中间件,该中间件查看请求路径和文件扩展名,然后使用 res.sendFile 和 path.resolve 创建要响应的文件路径。

import { Injectable, NestMiddleware } from '@nestjs/common';
import { apiPrefix } from '../../config';
import { Request, Response } from 'express';
import { resolve } from 'path';

const allowedExtentions = [
  '.js',
  '.ico',
  '.css',
  '.png',
  '.jpg',
  '.woff2',
  '.woff',
  '.ttf',
  '.svg',
];

const prefix = apiPrefix;

@Injectable()
export class FrontendMiddleware implements NestMiddleware …
Run Code Online (Sandbox Code Playgroud)

heroku node.js angular nestjs

6
推荐指数
1
解决办法
5105
查看次数

标签 统计

angular ×1

heroku ×1

nestjs ×1

node.js ×1