Skip to content

Commit cebf06c

Browse files
committed
feat: made it work
1 parent ef96e5d commit cebf06c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+844
-1015
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ comes with a LSP for Vim users.
1212

1313
## Start the application
1414

15-
Run `npx nx serve bun-test` to start the development server. Happy coding!
15+
Run `npx nx serve app` to start the development server. Happy coding!
1616

1717
## Build for production
1818

19-
Run `npx nx build bun-test` to build the application. The build artifacts are stored in the output directory (e.g. `dist/` or `build/`), ready to be deployed.
19+
Run `npx nx build app` to build the application. The build artifacts are stored in the output directory (e.g. `dist/` or `build/`), ready to be deployed.
2020

2121
## Running tasks
2222

apps/api/.eslintrc.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"extends": ["../../.eslintrc.json"],
3+
"ignorePatterns": ["!**/*"],
4+
"overrides": [
5+
{
6+
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
7+
"rules": {}
8+
},
9+
{
10+
"files": ["*.ts", "*.tsx"],
11+
"rules": {}
12+
},
13+
{
14+
"files": ["*.js", "*.jsx"],
15+
"rules": {}
16+
}
17+
]
18+
}

apps/api/jest.config.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/* eslint-disable */
2+
export default {
3+
displayName: 'api',
4+
preset: '../../jest.preset.js',
5+
testEnvironment: 'node',
6+
transform: {
7+
'^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }],
8+
},
9+
moduleFileExtensions: ['ts', 'js', 'html'],
10+
coverageDirectory: '../../coverage/apps/api',
11+
};

apps/api/project.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "api",
3+
"$schema": "../../node_modules/nx/schemas/project-schema.json",
4+
"sourceRoot": "apps/api/src",
5+
"projectType": "application",
6+
"tags": ["scope:app"],
7+
"targets": {
8+
"serve": {
9+
"executor": "@nx/js:node",
10+
"defaultConfiguration": "development",
11+
"options": {
12+
"buildTarget": "api:build"
13+
},
14+
"configurations": {
15+
"development": {
16+
"buildTarget": "api:build:development"
17+
},
18+
"production": {
19+
"buildTarget": "api:build:production"
20+
}
21+
}
22+
},
23+
"test": {
24+
"executor": "@nx/jest:jest",
25+
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
26+
"options": {
27+
"jestConfig": "apps/api/jest.config.ts"
28+
}
29+
}
30+
}
31+
}

apps/api/src/main.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* This is not a production server yet!
3+
* This is only a minimal backend to get started.
4+
*/
5+
6+
import { Logger } from '@nestjs/common';
7+
import { NestFactory } from '@nestjs/core';
8+
9+
import { AppModule } from '@app/backend-root';
10+
11+
async function bootstrap() {
12+
const app = await NestFactory.create(AppModule);
13+
const globalPrefix = 'api';
14+
app.setGlobalPrefix(globalPrefix);
15+
const port = process.env.PORT || 3000;
16+
await app.listen(port);
17+
Logger.log(
18+
`🚀 Application is running on: http://localhost:${port}/${globalPrefix}`,
19+
);
20+
}
21+
22+
bootstrap();

apps/api/tsconfig.app.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
"outDir": "../../dist/out-tsc",
5+
"module": "commonjs",
6+
"types": ["node"],
7+
"emitDecoratorMetadata": true,
8+
"target": "es2021"
9+
},
10+
"exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"],
11+
"include": ["src/**/*.ts"]
12+
}

apps/api/tsconfig.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"extends": "../../tsconfig.base.json",
3+
"files": [],
4+
"include": [],
5+
"references": [
6+
{
7+
"path": "./tsconfig.app.json"
8+
},
9+
{
10+
"path": "./tsconfig.spec.json"
11+
}
12+
],
13+
"compilerOptions": {
14+
"esModuleInterop": true
15+
}
16+
}

apps/api/tsconfig.spec.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
"outDir": "../../dist/out-tsc",
5+
"module": "commonjs",
6+
"types": ["jest", "node"]
7+
},
8+
"include": [
9+
"jest.config.ts",
10+
"src/**/*.test.ts",
11+
"src/**/*.spec.ts",
12+
"src/**/*.d.ts"
13+
]
14+
}

apps/api/webpack.config.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const { NxAppWebpackPlugin } = require('@nx/webpack/app-plugin');
2+
const { join } = require('path');
3+
4+
module.exports = {
5+
output: {
6+
path: join(__dirname, '../../dist/apps/api'),
7+
},
8+
plugins: [
9+
new NxAppWebpackPlugin({
10+
target: 'node',
11+
compiler: 'tsc',
12+
main: './src/main.ts',
13+
tsConfig: './tsconfig.app.json',
14+
optimization: false,
15+
outputHashing: 'none',
16+
}),
17+
],
18+
};
File renamed without changes.

apps/bun-test-e2e/playwright.config.ts renamed to apps/app-e2e/playwright.config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ export default defineConfig({
2525
},
2626
/* Run your local dev server before starting the tests */
2727
webServer: {
28-
command: 'bun nx serve bun-test',
28+
command: 'bun nx serve app',
2929
url: 'http://localhost:4200',
30-
reuseExistingServer: !process.env.CI,
30+
reuseExistingServer: !process.env['CI'],
3131
cwd: workspaceRoot,
3232
},
3333
projects: [

apps/app-e2e/project.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "app-e2e",
3+
"$schema": "../../node_modules/nx/schemas/project-schema.json",
4+
"projectType": "application",
5+
"sourceRoot": "apps/app-e2e/src",
6+
"implicitDependencies": ["app"],
7+
"// targets": "to see all targets run: nx show project app-e2e --web",
8+
"targets": {}
9+
}
File renamed without changes.
File renamed without changes.

apps/bun-test/jest.config.ts renamed to apps/app/jest.config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/* eslint-disable */
22
export default {
3-
displayName: 'bun-test',
3+
displayName: 'app',
44
preset: '../../jest.preset.js',
55
setupFilesAfterEnv: ['<rootDir>/src/test-setup.ts'],
6-
coverageDirectory: '../../coverage/apps/bun-test',
6+
coverageDirectory: '../../coverage/apps/app',
77
transform: {
88
'^.+\\.(ts|mjs|js|html)$': [
99
'jest-preset-angular',

apps/bun-test/project.json renamed to apps/app/project.json

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,45 @@
11
{
2-
"name": "bun-test",
2+
"name": "app",
33
"$schema": "../../node_modules/nx/schemas/project-schema.json",
44
"projectType": "application",
55
"prefix": "app",
6-
"sourceRoot": "apps/bun-test/src",
7-
"tags": [],
6+
"sourceRoot": "apps/app/src",
7+
"tags": ["scope:app"],
88
"targets": {
99
"build": {
1010
"executor": "@angular-devkit/build-angular:application",
1111
"outputs": ["{options.outputPath}"],
1212
"options": {
13-
"outputPath": "dist/apps/bun-test",
14-
"index": "apps/bun-test/src/index.html",
15-
"browser": "apps/bun-test/src/main.ts",
13+
"outputPath": "dist/apps/app",
14+
"index": "apps/app/src/index.html",
15+
"browser": "apps/app/src/main.ts",
1616
"polyfills": ["zone.js"],
17-
"tsConfig": "apps/bun-test/tsconfig.app.json",
17+
"tsConfig": "apps/app/tsconfig.app.json",
1818
"inlineStyleLanguage": "scss",
1919
"assets": [
2020
{
2121
"glob": "**/*",
22-
"input": "apps/bun-test/public"
22+
"input": "apps/app/public"
2323
}
2424
],
25-
"styles": ["apps/bun-test/src/styles.scss"],
25+
"styles": [
26+
"@angular/material/prebuilt-themes/magenta-violet.css",
27+
"apps/app/src/styles.scss"
28+
],
29+
"externalDependencies": [
30+
"@nestjs/microservices",
31+
"@nestjs/microservices/microservices-module",
32+
"@nestjs/websockets",
33+
"@nestjs/websockets/socket-module",
34+
"cache-manager",
35+
"class-transformer",
36+
"class-validator"
37+
],
2638
"scripts": [],
27-
"server": "apps/bun-test/src/main.server.ts",
39+
"server": "apps/app/src/main.server.ts",
2840
"prerender": true,
2941
"ssr": {
30-
"entry": "apps/bun-test/server.ts"
42+
"entry": "apps/app/server/server.ts"
3143
}
3244
},
3345
"configurations": {
@@ -58,18 +70,19 @@
5870
"executor": "@angular-devkit/build-angular:dev-server",
5971
"configurations": {
6072
"production": {
61-
"buildTarget": "bun-test:build:production"
73+
"buildTarget": "app:build:production"
6274
},
6375
"development": {
64-
"buildTarget": "bun-test:build:development"
76+
"buildTarget": "app:build:development",
77+
"proxyConfig": "apps/app/proxy.conf.json"
6578
}
6679
},
6780
"defaultConfiguration": "development"
6881
},
6982
"extract-i18n": {
7083
"executor": "@angular-devkit/build-angular:extract-i18n",
7184
"options": {
72-
"buildTarget": "bun-test:build"
85+
"buildTarget": "app:build"
7386
}
7487
},
7588
"lint": {
@@ -79,7 +92,7 @@
7992
"executor": "@nx/jest:jest",
8093
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
8194
"options": {
82-
"jestConfig": "apps/bun-test/jest.config.ts"
95+
"jestConfig": "apps/app/jest.config.ts"
8396
}
8497
}
8598
}

apps/app/proxy.conf.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"/api": {
3+
"target": "http://localhost:3000",
4+
"secure": false
5+
}
6+
}
File renamed without changes.

apps/app/server/main.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { NestFactory } from '@nestjs/core';
2+
import { AppModule } from '@app/backend-root';
3+
import { Logger } from '@nestjs/common';
4+
5+
export default async function bootstrap() {
6+
const app = await NestFactory.create(AppModule);
7+
app.enableCors({
8+
methods: 'GET',
9+
maxAge: 3600
10+
});
11+
app.setGlobalPrefix('api');
12+
const port = process.env['PORT'] || 3000;
13+
14+
await app.listen(port);
15+
16+
Logger.log(`Server running on http://localhost:${port}`, 'Bootstrap');
17+
}
18+
19+
bootstrap();

apps/bun-test/server.ts renamed to apps/app/server/server.ts

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@ import { CommonEngine } from '@angular/ssr';
33
import express from 'express';
44
import { fileURLToPath } from 'node:url';
55
import { dirname, join, resolve } from 'node:path';
6-
import bootstrap from './src/main.server';
6+
import bootstrap from '../src/main.server';
7+
import { NestFactory } from '@nestjs/core';
8+
import { AppModule } from '@app/backend-root';
9+
import { ExpressAdapter } from '@nestjs/platform-express';
710

811
// The Express app is exported so that it can be used by serverless Functions.
9-
export function app(): express.Express {
12+
export async function app(): Promise<express.Express> {
1013
const server = express();
1114
const serverDistFolder = dirname(fileURLToPath(import.meta.url));
1215
const browserDistFolder = resolve(serverDistFolder, '../browser');
@@ -19,6 +22,7 @@ export function app(): express.Express {
1922

2023
// Example Express Rest API endpoints
2124
// server.get('/api/**', (req, res) => { });
25+
2226
// Serve static files from /browser
2327
server.get(
2428
'**',
@@ -28,30 +32,42 @@ export function app(): express.Express {
2832
})
2933
);
3034

31-
// All regular routes use the Angular engine
35+
// All regular routes use the Angular engine for rendering
36+
// except for /api/** routes
3237
server.get('**', (req, res, next) => {
3338
const { protocol, originalUrl, baseUrl, headers } = req;
3439

35-
commonEngine
36-
.render({
37-
bootstrap,
38-
documentFilePath: indexHtml,
39-
url: `${protocol}://${headers.host}${originalUrl}`,
40-
publicPath: browserDistFolder,
41-
providers: [{ provide: APP_BASE_HREF, useValue: baseUrl }],
42-
})
43-
.then((html) => res.send(html))
44-
.catch((err) => next(err));
40+
if (req.originalUrl.startsWith('/api')) {
41+
// Handle API routes separately
42+
next();
43+
} else {
44+
commonEngine
45+
.render({
46+
bootstrap,
47+
documentFilePath: indexHtml,
48+
url: `${protocol}://${headers.host}${originalUrl}`,
49+
publicPath: browserDistFolder,
50+
providers: [{ provide: APP_BASE_HREF, useValue: baseUrl }],
51+
})
52+
.then((html) => res.send(html))
53+
.catch((err) => next(err));
54+
}
4555
});
4656

57+
// setup NestJS app
58+
const adapter = new ExpressAdapter(server);
59+
const app = await NestFactory.create(AppModule, adapter);
60+
app.setGlobalPrefix('api');
61+
await app.init();
62+
4763
return server;
4864
}
4965

50-
function run(): void {
66+
async function run(): Promise<void> {
5167
const port = process.env['PORT'] || 4000;
5268

5369
// Start up the Node server
54-
const server = app();
70+
const server = await app();
5571
server.listen(port, () => {
5672
console.log(`Node Express server listening on http://localhost:${port}`);
5773
});

0 commit comments

Comments
 (0)