@nestjs/ng-universal
Nest - modern, fast, powerful node.js web framework (@ng-universal)
Last updated 3 months ago by nestjscore .
MIT · Original npm · Tarball · package.json
$ cnpm install @nestjs/ng-universal 
SYNC missed versions from official npm registry.

Nest Logo

A progressive Node.js framework for building efficient and scalable server-side applications.

NPM Version Package License NPM Downloads Travis Linux Coverage Discord Backers on Open Collective Sponsors on Open Collective

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
cache boolean? | object? Cache options, description below (default: true)
errorHandler Function? Callback to be called in case of a rendering error

Cache

Property Type Description
expiresIn number? Cache expiration in milliseconds (default: 60000)
storage CacheStorage? Interface for implementing custom cache storage (default: in memory)
keyGenerator CacheKeyGenerator? Interface for implementing custom cache key generation logic (default: by url)
AngularUniversalModule.forRoot({
  bootstrap: AppServerModule,
  viewsPath: join(process.cwd(), 'dist/{APP_NAME}/browser'),
  cache: {
    storage: new InMemoryCacheStorage(),
    expiresIn: DEFAULT_CACHE_EXPIRATION_TIME,
    keyGenerator: new CustomCacheKeyGenerator()
  }
});

Example for CacheKeyGenerator:

export class CustomCacheKeyGenerator implements CacheKeyGenerator {
  generateCacheKey(request: Request): string {
    const md = new MobileDetect(request.headers['user-agent']);
    const isMobile = md.mobile() ? 'mobile' : 'desktop';
    return (request.hostname + request.originalUrl + isMobile).toLowerCase();
  }
}

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. Note that tokens must be imported from the @nestjs/ng-universal/tokens, not @nguniversal/express-engine/tokens.

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 '@nestjs/ng-universal/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

License

Nest is MIT licensed.

Current Tags

  • 4.1.1                                ...           latest (3 months ago)
  • 2.0.1-next.12                                ...           next (a year ago)

97 Versions

  • 4.1.1                                ...           3 months ago
  • 4.1.0                                ...           3 months ago
  • 4.0.1                                ...           5 months ago
  • 4.0.0                                ...           5 months ago
  • 3.2.0                                ...           8 months ago
  • 3.1.0                                ...           10 months ago
  • 3.0.1                                ...           a year ago
  • 3.0.0                                ...           a year ago
  • 2.0.1-next.12                                ...           a year ago
  • 2.0.1-next.11                                ...           a year ago
  • 2.0.1-next.10                                ...           a year ago
  • 2.0.1-next.9                                ...           a year ago
  • 2.0.1-next.8                                ...           a year ago
  • 2.0.1-next.7                                ...           a year ago
  • 2.0.1-next.6                                ...           a year ago
  • 2.0.1-next.5                                ...           a year ago
  • 2.0.1-next.4                                ...           a year ago
  • 2.0.1-next.3                                ...           a year ago
  • 2.0.1-next.2                                ...           a year ago
  • 2.0.1-next.1                                ...           a year ago
  • 2.0.1                                ...           a year ago
  • 2.0.0                                ...           2 years ago
  • 2.0.0-next.2                                ...           2 years ago
  • 2.0.0-next.1                                ...           2 years ago
  • 1.2.0                                ...           2 years ago
  • 1.2.0-next.3                                ...           2 years ago
  • 1.2.0-next.2                                ...           2 years ago
  • 1.2.0-next.1                                ...           2 years ago
  • 1.1.1                                ...           2 years ago
  • 1.1.0                                ...           2 years ago
  • 1.0.2                                ...           2 years ago
  • 1.0.1                                ...           2 years ago
  • 1.0.0-next.2                                ...           2 years ago
  • 1.0.0-next.1                                ...           2 years ago
  • 1.0.0                                ...           2 years ago
  • 0.8.0-next.3                                ...           2 years ago
  • 0.8.0-next.2                                ...           2 years ago
  • 0.8.0                                ...           2 years ago
  • 0.7.1                                ...           2 years ago
  • 0.6.0-next.8                                ...           2 years ago
  • 0.6.0-next.7                                ...           2 years ago
  • 0.6.0-next.6                                ...           2 years ago
  • 0.6.0-next.5                                ...           2 years ago
  • 0.6.0-next.4                                ...           2 years ago
  • 0.6.0-next.3                                ...           2 years ago
  • 0.6.0-next.1                                ...           2 years ago
  • 0.6.0-next                                ...           2 years ago
  • 0.6.0                                ...           2 years ago
  • 0.5.1                                ...           2 years ago
  • 0.5.0                                ...           2 years ago
  • 0.4.2-beta.38                                ...           2 years ago
  • 0.4.2-beta.37                                ...           2 years ago
  • 0.4.2-beta.36                                ...           2 years ago
  • 0.4.2-beta.35                                ...           2 years ago
  • 0.4.2-beta.32                                ...           2 years ago
  • 0.4.2-beta.31                                ...           2 years ago
  • 0.4.2-beta.30                                ...           2 years ago
  • 0.4.2-beta.29                                ...           2 years ago
  • 0.4.2-beta.28                                ...           2 years ago
  • 0.4.2-beta.27                                ...           2 years ago
  • 0.4.2-beta.26                                ...           2 years ago
  • 0.4.2-beta.25                                ...           2 years ago
  • 0.4.2-beta.24                                ...           2 years ago
  • 0.4.2-beta.23                                ...           2 years ago
  • 0.4.2-beta.22                                ...           2 years ago
  • 0.4.2-beta.21                                ...           2 years ago
  • 0.4.2-beta.20                                ...           2 years ago
  • 0.4.2-beta.19                                ...           2 years ago
  • 0.4.2-beta.18                                ...           2 years ago
  • 0.4.2-beta.17                                ...           2 years ago
  • 0.4.2-beta.16                                ...           2 years ago
  • 0.4.2-beta.15                                ...           2 years ago
  • 0.4.2-beta.14                                ...           2 years ago
  • 0.4.2-beta.13                                ...           2 years ago
  • 0.4.2-beta.12                                ...           2 years ago
  • 0.4.2-beta.11                                ...           2 years ago
  • 0.4.2-beta.10                                ...           2 years ago
  • 0.4.2-beta.9                                ...           2 years ago
  • 0.4.2-beta.8                                ...           2 years ago
  • 0.4.2-beta.7                                ...           2 years ago
  • 0.4.2-beta.6                                ...           2 years ago
  • 0.4.2-beta.5                                ...           2 years ago
  • 0.4.2-beta.4                                ...           2 years ago
  • 0.4.2-beta.3                                ...           2 years ago
  • 0.4.2-beta.2                                ...           2 years ago
  • 0.4.2-beta.1                                ...           2 years ago
  • 0.4.2                                ...           2 years ago
  • 0.4.1                                ...           2 years ago
  • 0.4.0                                ...           2 years ago
  • 0.3.0                                ...           2 years ago
  • 0.2.0                                ...           2 years ago
  • 0.1.1                                ...           2 years ago
  • 0.1.0                                ...           2 years ago
  • 0.0.4                                ...           2 years ago
  • 0.0.3                                ...           2 years ago
  • 0.0.2                                ...           2 years ago
  • 0.0.1                                ...           2 years ago
Downloads
Today 0
This Week 2
This Month 20
Last Day 2
Last Week 6
Last Month 51
Dev Dependencies (13)

Copyright 2014 - 2017 © taobao.org |