Intro
This time, I added server-side.I choosed Nest.js because I love TypeScript and I had been interested in using TypeScript in server-side programming.
Environments
- Angular ver.10.0.2
- @ngrx/store ver.9.2.0
- @ngrx/effects ver.9.2.0
- @nestjs/ng-universal ver.2.0.1
Couldn't add @ngrx/store into the Angular 10 project
First, I created an Angular project and tried adding @ngrx/store into it. And I got an error.Installing packages for tooling via npm. Installed packages for tooling via npm. The package that you are trying to add does not support schematics. You can try using a different version of the package or contact the package author to add ng-add support.According to the issue below, I should add "@latest".
npx ng add @ngrx/store@latestBecause if I didn't add it, the @ngrx/store version was too low.
Though I didn't know I should add "@latest" into "@ngrx/effects" or not, I installed it by "npx ng add @ngrx/effects@latest"
Add a Nest.js project
I added a Nest.js project by "@nestjs/ng-universal". It was for Server-Side Rendering for Nest.js.
After "ng add" command, I input the Angular project name.
npx ng add @nestjs/ng-universal
Run the project (Failed)
I could run the project by "npm run serve" and access the page from "localhost:4200". But when I opened the page, I got an error.... [Nest] 12736 - 07/08/2020, 1:04:45 AM [ExceptionsHandler] Cannot read property 'indexOf' of undefined +28479ms TypeError: Cannot read property 'indexOf' of undefined ...According to the issue below, I set "liveReload" false.
TypeError: Cannot read property 'indexOf' of undefined · Issue #188 · nestjs/ng-universal · GitHub
server/app.module.ts
import { Module } from '@nestjs/common';
import { AngularUniversalModule } from '@nestjs/ng-universal';
import { join } from 'path';
@Module({
imports: [
AngularUniversalModule.forRoot({
viewsPath: join(process.cwd(), 'dist/browser'),
bundle: require('../server/main'),
liveReload: false
})
]
})
export class ApplicationModule {}
Show a white page
Though I could avoid the error, I couldn't watch the Angular project page but a white page. Because the properties of "AngularUniversalOptions" were changed, I couldn't use "bootstrap" like README.md. But when I accessed "localhost:4200/index.html", I could watch the Angular project page.
server/app.module.ts
import { Module } from '@nestjs/common';
import { AngularUniversalModule } from '@nestjs/ng-universal';
import { join } from 'path';
@Module({
imports: [
AngularUniversalModule.forRoot({
viewsPath: join(process.cwd(), 'dist/browser'),
bundle: require('../server/main'),
liveReload: false,
templatePath: join(process.cwd(), 'dist/browser/index.html'),
})
]
})
export class ApplicationModule {}
Should I redirect by "@nestjs/serve-static" or by routing?
Add @nestjs/serve-static
In this time, I choosed installing "@nestjs/serve-static" to show "index.html".npm install --save @nestjs/serve-static
server/main.ts
import { NestFactory } from '@nestjs/core';
import { ApplicationModule } from './app.module';
import { NestExpressApplication } from '@nestjs/platform-express';
import { join } from 'path';
async function bootstrap() {
const app = await NestFactory.create(ApplicationModule);
app.setGlobalPrefix('api');
app.useStaticAssets(join(__dirname, '..', 'browser'));
await app.listen(4200);
}
bootstrap();
コメント
コメントを投稿