16.2.0 • Published 8 months ago

@nguniversal/express-engine v16.2.0

Weekly downloads
119,442
License
MIT
Repository
github
Last release
8 months 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-648ng-universal-15ng-ssrnode-angular-app-templatengx-cookie-ggyng-abtestnotationpwangx-fire-cookiengextnguniversal-expressengine-httpinterceptorngpacab-shared-imi-v142angular-ab-testsangular-nest-starterangular-plugin-architectureangular-ssr-startup@zalastax/nolb-_ngu@arsloptima/password-strengthasi-ngtools-app@daybreaksoft/angular-elastic-ui@builder.io/angularcap-angular-schematic-ssrohayolibsnx-serverless-reomni-checkpyger-app1pp2pc-packagesearch-userssenz-sspvmdkmksvictop-front-end-framewinter-ng-universal-plugintest-angular-elastic-uitbsoft.appservertematikbagisspa-shared-lib@cdutils/apptoolkit@coraledge/nx-serverlessanimate-on-scroll-sample-project@engineers/ngx-universal-express@house-of-angular/nestjs-ng-pdf-generator@hmcts/annotation-uidelon-fork@kerr-srl/ngx-cookiefe-angular-global@kai1015/application@kai1015/frontend-sls-deploygdti-crmfm-radio@meteora-pro/nestjs-ng-universal-async-cachefusebox-angular-universal-starterfusing-angularfusing-angular-clighost-express-servergnw-compliance-checker-rules@nhammond101/nx-serverless@nestjs-contrib/ng-universal@netlify/plugin-angular-universal@nestjs/ng-universal@nestjs-fixed/ng-universal@nxarch/nest-nguniversal@ng-toolkit/universalflowaccountham-app-universal@pixelmatic/blogincognito-sspimeepos-framework@shumih/ng-universal@terragon/terragoniwe10@rucken/core@spvcoder/ng-universal@spvcoder/ng-universal-next@spartacus/setup@spartacus/fsa-schematics@spartacus/schematics@spielhalle/client@tolstenko/ng-universalkre-formlocalize-router-ssr@typijs/core@valueadd/nestjs-ng-pdf-generator@verdocs/angular-api@verdocs/search
16.1.3

8 months ago

16.1.2

8 months ago

16.2.0-rc.0

8 months ago

16.2.0

8 months ago

16.1.1

9 months ago

16.1.0

10 months ago

16.1.0-rc.0

10 months ago

16.1.0-next.0

10 months ago

16.0.2

11 months ago

16.0.1

11 months ago

16.0.0

11 months ago

16.0.0-rc.0

12 months ago

16.0.0-rc.1

11 months ago

16.0.0-rc.2

11 months ago

16.0.0-next.0

12 months ago

15.2.1

12 months ago

15.2.0-rc.0

1 year ago

15.2.0

1 year ago

15.1.0

1 year ago

15.1.0-rc.0

1 year ago

15.0.0

1 year ago

14.2.2

1 year ago

14.2.3

1 year ago

15.0.0-rc.0

1 year ago

15.0.0-rc.1

1 year ago

14.2.1

1 year ago

15.0.0-next.1

1 year ago

15.0.0-next.0

1 year ago

14.2.0

2 years ago

14.2.0-rc.0

2 years ago

14.1.0-rc.0

2 years ago

14.1.0

2 years ago

14.1.0-next.2

2 years ago

14.1.0-next.1

2 years ago

14.1.0-next.0

2 years ago

14.0.1

2 years ago

14.0.2

2 years ago

14.0.3

2 years ago

14.2.0-next.0

2 years ago

13.1.1

2 years ago

14.0.0-next.0

2 years ago

14.0.0-rc.1

2 years ago

14.0.0-rc.0

2 years ago

14.0.0

2 years ago

13.1.0

2 years ago

13.0.2

2 years ago

13.1.0-next.1

2 years ago

13.0.1

2 years ago

13.0.0

2 years ago

13.0.0-rc.2

2 years ago

12.1.3

2 years ago

13.1.0-next.0

2 years ago

12.1.2

2 years ago

13.0.0-rc.1

2 years ago

13.0.0-rc.0

2 years ago

12.1.1

2 years ago

12.1.0

3 years ago

12.1.0-next.0

3 years ago

12.0.2

3 years ago

12.0.1

3 years ago

12.0.0

3 years ago

12.0.0-rc.1

3 years ago

12.0.0-rc.0

3 years ago

12.0.0-next.1

3 years ago

11.2.1

3 years ago

12.0.0-next.0

3 years ago

11.2.0

3 years ago

11.1.2

3 years ago

11.1.1

3 years ago

11.1.0

3 years ago

11.1.0-next.0

3 years ago

11.0.1

3 years ago

11.0.0

3 years ago

11.0.0-rc.0

3 years ago

11.0.0-next.2

3 years ago

11.0.0-next.1

4 years ago

11.0.0-next.0

4 years ago

10.1.0

4 years ago

10.1.0-rc.0

4 years ago

10.0.2

4 years ago

10.0.1

4 years ago

10.0.0

4 years ago

10.0.0-rc.1

4 years ago

10.0.0-rc.0

4 years ago

10.0.0-next.0

4 years ago

9.1.1

4 years ago

9.1.0

4 years ago

9.0.2

4 years ago

9.0.1

4 years ago

9.0.0

4 years ago

9.0.0-rc.2

4 years ago

9.0.0-rc.1

4 years ago

9.0.0-rc.0

4 years ago

9.0.0-next.14

4 years ago

9.0.0-next.13

4 years ago

8.2.6

4 years ago

8.2.5

4 years ago

9.0.0-next.9

4 years ago

9.0.0-next.8

4 years ago

9.0.0-next.7

4 years ago

9.0.0-next.6

4 years ago

9.0.0-next.5

4 years ago

9.0.0-next.3

4 years ago

9.0.0-next.2

5 years ago

9.0.0-next.1

5 years ago

9.0.0-next.0

5 years ago

8.1.1

5 years ago

8.1.0

5 years ago

8.0.0-rc.1

5 years ago

8.0.0-rc.0

5 years ago

8.0.0-beta.0

5 years ago

7.1.1

5 years ago

7.1.0

5 years ago

0.0.0

5 years ago

7.0.2

5 years ago

7.0.1

5 years ago

7.0.0

5 years ago

7.0.0-rc.0

5 years ago

6.1.0

6 years ago

6.0.0

6 years ago

6.0.0-rc.2

6 years ago

6.0.0-rc.1

6 years ago

6.0.0-rc.0

6 years ago

5.0.0

6 years ago

5.0.0-beta.8

6 years ago

5.0.0-beta.7

6 years ago

5.0.0-beta.6

6 years ago

5.0.0-beta.5

6 years ago

5.0.0-beta.4

6 years ago

5.0.0-beta.3

6 years ago

5.0.0-beta.2

6 years ago

5.0.0-beta.1

7 years ago

5.0.0-beta.0

7 years ago

1.0.0-beta.3

7 years ago

1.0.0-beta.2

7 years ago

1.0.0-beta.1

7 years ago

1.0.0-beta.0

7 years ago