16.2.0 • Published 2 years ago

@nguniversal/express-engine v16.2.0

Weekly downloads
119,442
License
MIT
Repository
github
Last release
2 years ago

Angular Express Engine

This is an Express Engine for running Angular Apps on the server for server side rendering.

Usage

npm install @nguniversal/express-engine --save

To use it, set the engine and then route requests to it

import express from 'express';
import { ngExpressEngine } from '@nguniversal/express-engine';

const app = express();

// Set the engine
app.engine(
  'html',
  ngExpressEngine({
    bootstrap: ServerAppModule, // Give it a module to bootstrap
  }),
);

app.set('view engine', 'html');

app.get('/**/*', (req: Request, res: Response) => {
  res.render('../dist/index', {
    req,
    res,
  });
});

Configuring the URL and Document

It is possible to override the default URL and document fetched when the rendering engine is called. To do so, simply pass in a url and/or document string to the renderer as follows:

app.get('/**/*', (req: Request, res: Response) => {
  let url = 'http://someurl.com';
  let doc = '<html><head><title>New doc</title></head></html>';
  res.render('../dist/index', {
    req,
    res,
    url,
    document: doc,
  });
});

Extra Providers

Extra Providers can be provided either on engine setup

app.engine(
  'html',
  ngExpressEngine({
    bootstrap: ServerAppModule,
    providers: [ServerService],
  }),
);

Advanced Usage

Request based Bootstrap

The Bootstrap module as well as more providers can be passed on request

app.get('/**/*', (req: Request, res: Response) => {
  res.render('../dist/index', {
    req,
    res,
    bootstrap: OtherServerAppModule,
    providers: [OtherServerService],
  });
});

Using the Request and Response

The Request and Response objects are injected into the app via injection tokens. You can access them by @Inject

import { Request } from 'express';
import { REQUEST } from '@nguniversal/express-engine/tokens';

@Injectable()
export class RequestService {
  constructor(@Inject(REQUEST) private request: Request) {}
}

If your app runs on the client side too, you will have to provide your own versions of these in the client app.

Using a Custom Callback

You can also use a custom callback to better handle your errors

app.get('/**/*', (req: Request, res: Response) => {
  res.render(
    '../dist/index',
    {
      req,
      res,
    },
    (err: Error, html: string) => {
      res.status(html ? 200 : 500).send(html || err.message);
    },
  );
});
@ljmarquezge2y/worldpay-spartacuslaji-frontplanner-projectcoderbase-monorepo-librairieten-thousandng-wc-frontendohayojpnebular-angular-seedngpacab-shared-imi-v124bnqczz_delonyelon@happystagger/web@davidlj95/website@davidlj95/website-v2angular2-multiselect-dropdown-oe@everything-registry/sub-chunk-648@spvcoder/ng-universal@spvcoder/ng-universal-nexttbsoft.appservertest-angular-elastic-uiwinter-ng-universal-plugintematikbagisvmdkmksvictop-front-end-frame@coraledge/nx-serverless@rucken/coreiwe10@pib_tech/commonspa-shared-lib@arsloptima/password-strength@cdutils/apptoolkit@daybreaksoft/angular-elastic-ui@meteora-pro/nestjs-ng-universal-async-cache@netlify/plugin-angular-universal@nestjs/ng-universal@ng-toolkit/universal@nhammond101/nx-serverless@engineers/ngx-universal-express@house-of-angular/nestjs-ng-pdf-generator@hmcts/annotation-ui@kai1015/application@kai1015/frontend-sls-deploy@kerr-srl/ngx-cookie@pixelmatic/blog@nxarch/nest-nguniversal@spielhalle/client@spartacus/fsa-schematics@spartacus/schematics@spartacus/setupomni-checknx-serverless-reohayolibspc-packagesearch-userspygesenz-sspnode-angular-app-templateng-universal-15nguniversal-expressengine-httpinterceptorngx-fire-cookieng-abtestng-ssrngextngpacab-shared-imi-v142ngx-cookie-ggynotationpwapp2r-app1shared-encoding-librarygnw-compliance-checker-rulesghost-express-serverimeepos-frameworkincognito-sspham-app-universallocalize-router-ssrkre-form@waitaction/nestjs-ng-universal@verdocs/angular-api@verdocs/search@valueadd/nestjs-ng-pdf-generator@worldpay2020/spartacus@typijs/core@tolstenko/ng-universal@terragon/terragon@zalastax/nolb-_nguangular-ab-testsangular-nest-starterangular-ssr-startupanimate-on-scroll-sample-projectangular-plugin-architectureasi-ngtools-appcap-angular-schematic-ssrdelon-forkfe-angular-globalfm-radioflowaccount@shumih/ng-universalgdti-crmfusebox-angular-universal-starterfusing-angular
16.1.3

2 years ago

16.1.2

2 years ago

16.2.0-rc.0

2 years ago

16.2.0

2 years ago

16.1.1

2 years ago

16.1.0

2 years ago

16.1.0-rc.0

2 years ago

16.1.0-next.0

2 years ago

16.0.2

2 years ago

16.0.1

2 years ago

16.0.0

2 years ago

16.0.0-rc.0

2 years ago

16.0.0-rc.1

2 years ago

16.0.0-rc.2

2 years ago

16.0.0-next.0

2 years ago

15.2.1

2 years ago

15.2.0-rc.0

2 years ago

15.2.0

2 years ago

15.1.0

2 years ago

15.1.0-rc.0

2 years ago

15.0.0

3 years ago

14.2.2

3 years ago

14.2.3

3 years ago

15.0.0-rc.0

3 years ago

15.0.0-rc.1

3 years ago

14.2.1

3 years ago

15.0.0-next.1

3 years ago

15.0.0-next.0

3 years ago

14.2.0

3 years ago

14.2.0-rc.0

3 years ago

14.1.0-rc.0

3 years ago

14.1.0

3 years ago

14.1.0-next.2

3 years ago

14.1.0-next.1

3 years ago

14.1.0-next.0

3 years ago

14.0.1

3 years ago

14.0.2

3 years ago

14.0.3

3 years ago

14.2.0-next.0

3 years ago

13.1.1

3 years ago

14.0.0-next.0

3 years ago

14.0.0-rc.1

3 years ago

14.0.0-rc.0

3 years ago

14.0.0

3 years ago

13.1.0

3 years ago

13.0.2

3 years ago

13.1.0-next.1

3 years ago

13.0.1

4 years ago

13.0.0

4 years ago

13.0.0-rc.2

4 years ago

12.1.3

4 years ago

13.1.0-next.0

4 years ago

12.1.2

4 years ago

13.0.0-rc.1

4 years ago

13.0.0-rc.0

4 years ago

12.1.1

4 years ago

12.1.0

4 years ago

12.1.0-next.0

4 years ago

12.0.2

4 years ago

12.0.1

4 years ago

12.0.0

4 years ago

12.0.0-rc.1

4 years ago

12.0.0-rc.0

4 years ago

12.0.0-next.1

4 years ago

11.2.1

4 years ago

12.0.0-next.0

4 years ago

11.2.0

4 years ago

11.1.2

4 years ago

11.1.1

4 years ago

11.1.0

4 years ago

11.1.0-next.0

5 years ago

11.0.1

5 years ago

11.0.0

5 years ago

11.0.0-rc.0

5 years ago

11.0.0-next.2

5 years ago

11.0.0-next.1

5 years ago

11.0.0-next.0

5 years ago

10.1.0

5 years ago

10.1.0-rc.0

5 years ago

10.0.2

5 years ago

10.0.1

5 years ago

10.0.0

5 years ago

10.0.0-rc.1

5 years ago

10.0.0-rc.0

5 years ago

10.0.0-next.0

5 years ago

9.1.1

5 years ago

9.1.0

5 years ago

9.0.2

5 years ago

9.0.1

5 years ago

9.0.0

5 years ago

9.0.0-rc.2

5 years ago

9.0.0-rc.1

5 years ago

9.0.0-rc.0

5 years ago

9.0.0-next.14

5 years ago

9.0.0-next.13

5 years ago

8.2.6

6 years ago

8.2.5

6 years ago

9.0.0-next.9

6 years ago

9.0.0-next.8

6 years ago

9.0.0-next.7

6 years ago

9.0.0-next.6

6 years ago

9.0.0-next.5

6 years ago

9.0.0-next.3

6 years ago

9.0.0-next.2

6 years ago

9.0.0-next.1

6 years ago

9.0.0-next.0

6 years ago

8.1.1

6 years ago

8.1.0

6 years ago

8.0.0-rc.1

6 years ago

8.0.0-rc.0

6 years ago

8.0.0-beta.0

6 years ago

7.1.1

6 years ago

7.1.0

6 years ago

0.0.0

6 years ago

7.0.2

7 years ago

7.0.1

7 years ago

7.0.0

7 years ago

7.0.0-rc.0

7 years ago

6.1.0

7 years ago

6.0.0

7 years ago

6.0.0-rc.2

7 years ago

6.0.0-rc.1

7 years ago

6.0.0-rc.0

7 years ago

5.0.0

7 years ago

5.0.0-beta.8

7 years ago

5.0.0-beta.7

7 years ago

5.0.0-beta.6

7 years ago

5.0.0-beta.5

8 years ago

5.0.0-beta.4

8 years ago

5.0.0-beta.3

8 years ago

5.0.0-beta.2

8 years ago

5.0.0-beta.1

8 years ago

5.0.0-beta.0

8 years ago

1.0.0-beta.3

8 years ago

1.0.0-beta.2

8 years ago

1.0.0-beta.1

8 years ago

1.0.0-beta.0

8 years ago