Skip to content

Commit ce06a3e

Browse files
authored
Enable backend to serve frontend (twentyhq#4461)
Basic POC to have frontend served by backend
1 parent 6704b08 commit ce06a3e

File tree

4 files changed

+50
-11
lines changed

4 files changed

+50
-11
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"@nestjs/jwt": "^10.0.3",
3535
"@nestjs/passport": "^9.0.3",
3636
"@nestjs/platform-express": "^9.0.0",
37-
"@nestjs/serve-static": "^3.0.0",
37+
"@nestjs/serve-static": "^4.0.1",
3838
"@nestjs/terminus": "^9.2.2",
3939
"@nestjs/typeorm": "^10.0.0",
4040
"@nivo/calendar": "^0.84.0",

packages/twenty-front/src/config/index.ts

+20-1
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,29 @@ declare global {
55
}
66
}
77

8+
const getDefaultUrl = () => {
9+
if (
10+
window.location.hostname === 'localhost' ||
11+
window.location.hostname === '127.0.0.1'
12+
) {
13+
// In development environment front and backend usually run on seperate ports
14+
// we set the default value to localhost:3000.
15+
// It dev context, we use env vars to overwrite it
16+
return 'http://localhost:3000';
17+
} else {
18+
// Outside of localhost we assume that they run on the same port
19+
// because the backend will serve the frontend
20+
// It prod context, we use env-config.js + window var to ovewrite it
21+
return `${window.location.protocol}//${window.location.hostname}${
22+
window.location.port ? `:${window.location.port}` : ''
23+
}`;
24+
}
25+
};
26+
827
export const REACT_APP_SERVER_BASE_URL =
928
window._env_?.REACT_APP_SERVER_BASE_URL ||
1029
process.env.REACT_APP_SERVER_BASE_URL ||
11-
'http://localhost:3000';
30+
getDefaultUrl();
1231

1332
export const REACT_APP_SERVER_AUTH_URL =
1433
window._env_?.REACT_APP_SERVER_AUTH_URL ||

packages/twenty-server/src/app.module.ts

+22-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
import { Module } from '@nestjs/common';
1+
import { DynamicModule, Module } from '@nestjs/common';
22
import { GraphQLModule } from '@nestjs/graphql';
33
import { ConfigModule } from '@nestjs/config';
4+
import { ServeStaticModule } from '@nestjs/serve-static';
5+
6+
import { existsSync } from 'fs';
7+
import { join } from 'path';
48

59
import { YogaDriver, YogaDriverConfig } from '@graphql-yoga/nestjs';
610

@@ -26,6 +30,22 @@ import { GraphQLConfigModule } from './graphql-config/graphql-config.module';
2630
IntegrationsModule,
2731
CoreModule,
2832
WorkspaceModule,
33+
...AppModule.getConditionalModules(),
2934
],
3035
})
31-
export class AppModule {}
36+
export class AppModule {
37+
private static getConditionalModules(): DynamicModule[] {
38+
const modules: DynamicModule[] = [];
39+
const frontPath = join(__dirname, '..', 'front');
40+
41+
if (existsSync(frontPath)) {
42+
modules.push(
43+
ServeStaticModule.forRoot({
44+
rootPath: frontPath,
45+
}),
46+
);
47+
}
48+
49+
return modules;
50+
}
51+
}

yarn.lock

+7-7
Original file line numberDiff line numberDiff line change
@@ -8134,15 +8134,15 @@ __metadata:
81348134
languageName: node
81358135
linkType: hard
81368136

8137-
"@nestjs/serve-static@npm:^3.0.0":
8138-
version: 3.0.1
8139-
resolution: "@nestjs/serve-static@npm:3.0.1"
8137+
"@nestjs/serve-static@npm:^4.0.1":
8138+
version: 4.0.1
8139+
resolution: "@nestjs/serve-static@npm:4.0.1"
81408140
dependencies:
81418141
path-to-regexp: "npm:0.2.5"
81428142
peerDependencies:
81438143
"@fastify/static": ^6.5.0
8144-
"@nestjs/common": ^9.0.0
8145-
"@nestjs/core": ^9.0.0
8144+
"@nestjs/common": ^9.0.0 || ^10.0.0
8145+
"@nestjs/core": ^9.0.0 || ^10.0.0
81468146
express: ^4.18.1
81478147
fastify: ^4.7.0
81488148
peerDependenciesMeta:
@@ -8152,7 +8152,7 @@ __metadata:
81528152
optional: true
81538153
fastify:
81548154
optional: true
8155-
checksum: 70d47863c37aeb5876fd12feda6a26299af928385dfc24e336f50f7ce9c692a4950c2da449937c5c771ce0bc3869cd1c442cc21b9916973e6e3fbfddb999f1d0
8155+
checksum: 940119197dd0f9d35db8d59eb871171f857c4507363e50c2a7e9a430dfbf1055fd3bc43969eb5d2863d01b8dd86ae093371bf64cbead343065a7643769455738
81568156
languageName: node
81578157
linkType: hard
81588158

@@ -45849,7 +45849,7 @@ __metadata:
4584945849
"@nestjs/passport": "npm:^9.0.3"
4585045850
"@nestjs/platform-express": "npm:^9.0.0"
4585145851
"@nestjs/schematics": "npm:^9.0.0"
45852-
"@nestjs/serve-static": "npm:^3.0.0"
45852+
"@nestjs/serve-static": "npm:^4.0.1"
4585345853
"@nestjs/terminus": "npm:^9.2.2"
4585445854
"@nestjs/testing": "npm:^9.0.0"
4585545855
"@nestjs/typeorm": "npm:^10.0.0"

0 commit comments

Comments
 (0)