@tolstenko/ng-universal
v3.1.0
Published
Nest - modern, fast, powerful node.js web framework (@ng-universal)
Downloads
2
Readme
Description
Angular Universal module for Nest.
Installation
Using the Angular CLI:
$ ng add @nestjs/ng-universal
Or manually:
$ npm i @nestjs/ng-universal
Example
See full example here.
Usage
If you have installed the module manually, you need to import AngularUniversalModule
in your Nest application.
import { Module } from '@nestjs/common';
import { join } from 'path';
import { AngularUniversalModule } from '@nestjs/ng-universal';
@Module({
imports: [
AngularUniversalModule.forRoot({
bootstrap: AppServerModule,
viewsPath: join(process.cwd(), 'dist/{APP_NAME}/browser')
}),
],
})
export class ApplicationModule {}
API Spec
The forRoot()
method takes an options object with a few useful properties.
| Property | Type | Description |
| ------------- | ------------- | ----- |
| viewsPath
| string | The directory where the module should look for client bundle (Angular app) |
| bootstrap
| Function | Angular server module reference (AppServerModule
). |
| templatePath
| string? | Path to index file (default: {viewsPaths}/index.html
) |
| rootStaticPath
| string? | Static files root directory (default: *.*
) |
| renderPath
| string? | Path to render Angular app (default: *
) |
| extraProviders
| StaticProvider[]? | The platform level providers for the current render request |
Request and Response Providers
This tool uses @nguniversal/express-engine
and will properly provide access to the Express Request and Response objects in you Angular components.
This is useful for things like setting the response code to 404 when your Angular router can't find a page (i.e. path: '**'
in routing):
import { Response } from 'express';
import { Component, Inject, Optional, PLATFORM_ID } from '@angular/core';
import { isPlatformServer } from '@angular/common';
import { RESPONSE } from '@nguniversal/express-engine/tokens';
@Component({
selector: 'my-not-found',
templateUrl: './not-found.component.html',
styleUrls: ['./not-found.component.scss'],
})
export class NotFoundComponent {
constructor(
@Inject(PLATFORM_ID)
private readonly platformId: any,
@Optional()
@Inject(RESPONSE)
res: Response,
) {
// `res` is the express response, only available on the server
if (isPlatformServer(this.platformId)) {
res.status(404);
}
}
}
Support
Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please read more here.
Stay in touch
- Author - Kamil Myśliwiec
- Website - https://nestjs.com
- Twitter - @nestframework
License
Nest is MIT licensed.