我正在使用NestJS应用程序中的app.useStaticAssets(ui);
为我的SPA提供服务。该应用程序正以kiosk模式在浏览器上运行。
新的kiosk硬件有两个屏幕,前面,后面。如果我们可以创建另一个SPA并在同一个NestJS应用程序上提供它,那将是最简单的。
我已经查看了NestJS文档,尝试检查源代码并搜索相关问题,使用res.sendFile (不确定资产)或在NestJS下使用express只有一些提示。
有没有办法在直通NestJS中做到这一点?
发布于 2019-07-03 00:06:05
如果使用Express作为HTTP适配器,则始终可以设置多个静态资产并对其使用不同的前缀
async function bootstrap() {
const app = await NestFactory.create<NestExpressApplication>(AppModule);
app.useStaticAssets(join(__dirname, '..', 'public'));
app.useStaticAssets( join(__dirname, '..', 'other'), {prefix: '/other/'});
await app.listen(3000);
}
bootstrap();
在这种情况下,如果有人导航到localhost:3000
,他们将获得public
文件夹的index.html
,但如果他们导航到localhost:3000/other
,他们将获得other
文件夹的index.html
。
发布于 2021-01-30 08:40:42
当尝试使用Jay答案中的代码时,它对我来说根本不起作用,因为我的Angular应用程序404'd中的路由直接通过服务器访问。这是因为内置的useStaticAssets()
函数只服务于它可以在给定目录中找到的文件,而不是您的Angular或React路由。
我强烈建议您查看@nestjs/serve-static,因为它允许您像这样为多个SPA提供服务:
import { Module } from '@nestjs/common';
import { ServeStaticModule } from '@nestjs/serve-static';
import { join as joinPath } from 'path';
@Module({
imports: [
ServeStaticModule.forRoot(
{
rootPath: joinPath(__dirname, '..', 'client-admin'),
serveRoot: '/admin'
},
{
rootPath: joinPath(__dirname, '..', 'client'),
}
)
]
})
export class AppModule {}
但是,在此之前,您需要确保您的SPA已配置为正确地安装在这些URL或资源(CSS/JS/等)上。将简单地使用404。例如,如果将管理应用程序挂载到/admin/
,则index.html
中的所有资源文件URL也必须以/admin/
为前缀。
https://stackoverflow.com/questions/56854290
复制相似问题