Updated api app to serve built frontend files
This commit is contained in:
parent
e5914b96c3
commit
489a332d2d
1 changed files with 20 additions and 6 deletions
|
|
@ -1,20 +1,34 @@
|
|||
/**
|
||||
* This is not a production server yet!
|
||||
* This is only a minimal backend to get started.
|
||||
*/
|
||||
|
||||
import { Logger } from '@nestjs/common';
|
||||
import { NestFactory } from '@nestjs/core';
|
||||
import { AppModule } from './app/app.module';
|
||||
import { join } from 'path';
|
||||
import * as express from 'express';
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
|
||||
async function bootstrap() {
|
||||
const app = await NestFactory.create(AppModule);
|
||||
|
||||
const globalPrefix = 'api';
|
||||
app.setGlobalPrefix(globalPrefix);
|
||||
|
||||
const server = app.getHttpAdapter().getInstance();
|
||||
|
||||
const distPath = join(__dirname, '..', 'web');
|
||||
server.use(express.static(distPath));
|
||||
|
||||
server.use((req: Request, res: Response, next: NextFunction) => {
|
||||
if (!req.path.startsWith(`/${globalPrefix}`)) {
|
||||
res.sendFile(join(distPath, 'index.html'));
|
||||
} else {
|
||||
next();
|
||||
}
|
||||
});
|
||||
|
||||
const port = process.env.PORT || 3000;
|
||||
await app.listen(port);
|
||||
|
||||
Logger.log(
|
||||
`🚀 Application is running on: http://localhost:${port}/${globalPrefix}`
|
||||
`🚀 Application is running on: http://localhost:${port}/ (API prefix: /${globalPrefix})`,
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue