@nguniversal/express-engine
TypeScript icon, indicating that this package has built-in type declarations

16.2.0 • Public • Published

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);
    },
  );
});

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
16.2.020,154latest
14.2.35,939v14-lts
15.2.15,914v15-lts
13.1.13,673v13-lts
16.2.0-rc.01next

Version History

VersionDownloads (Last 7 Days)Published
16.2.020,154
16.1.3198
16.2.0-rc.01
16.1.277
16.1.1874
16.1.0852
16.1.0-rc.01
16.1.0-next.01
16.0.2544
16.0.1118
16.0.0101
16.0.0-rc.22
16.0.0-rc.12
16.0.0-rc.04
15.2.15,914
16.0.0-next.03
15.2.0931
15.2.0-rc.011
15.1.0458
15.1.0-rc.03
14.2.35,939
15.0.0764
15.0.0-rc.11
15.0.0-rc.05
14.2.2121
15.0.0-next.11
14.2.154
15.0.0-next.03
14.2.0579
14.2.0-rc.05
14.2.0-next.02
14.1.068
14.1.0-rc.0151
14.1.0-next.25
14.0.3197
14.1.0-next.11
14.0.225
14.1.0-next.03
14.0.110
14.0.064
14.0.0-rc.10
14.0.0-rc.02
14.0.0-next.03
13.1.13,673
13.1.066
13.1.0-next.11
13.0.2387
13.0.1464
13.1.0-next.02
13.0.018
12.1.31,817
13.0.0-rc.21
12.1.28
13.0.0-rc.11
13.0.0-rc.04
12.1.170
12.1.01,007
12.0.2165
12.1.0-next.05
12.0.119
12.0.043
12.0.0-rc.10
12.0.0-rc.01
0.0.0-PLACEHOLDER2
12.0.0-next.11
12.0.0-next.01
11.2.11,379
11.2.023
11.1.265
11.1.154
11.1.05
11.1.0-next.02
11.0.1408
11.0.026
11.0.0-rc.03
11.0.0-next.22
11.0.0-next.11
11.0.0-next.03
10.1.01,267
10.1.0-rc.01
10.0.2111
10.0.11,335
10.0.07
10.0.0-rc.13
10.0.0-rc.01
9.1.11,205
10.0.0-next.02
9.1.0171
9.0.256
9.0.144
9.0.054
9.0.0-rc.21
9.0.0-rc.13
9.0.0-rc.00
9.0.0-next.142
9.0.0-next.131
8.2.61,979
8.2.52
9.0.0-next.90
9.0.0-next.83
9.0.0-next.71
9.0.0-next.64
9.0.0-next.53
9.0.0-next.31
9.0.0-next.22
9.0.0-next.12
9.0.0-next.06
8.1.1741
8.1.08
8.0.0-rc.112
8.0.0-rc.02
8.0.0-beta.02
7.1.12,789
7.1.0110
0.0.016
7.0.21,376
7.0.1196
7.0.012
7.0.0-rc.1.with-local-changes1
7.0.0-rc.02
6.1.0159
6.0.0339
6.0.0-rc.21
6.0.0-rc.12
6.0.0-rc.01
5.0.0131
5.0.0-beta.84
5.0.0-beta.545
5.0.0-beta.46
5.0.0-beta.30
5.0.0-beta.23
5.0.0-beta.115
5.0.0-beta.00
1.0.0-beta.3111
1.0.0-beta.224
1.0.0-beta.11
1.0.0-beta.01

Package Sidebar

Install

npm i @nguniversal/express-engine

Weekly Downloads

60,369

Version

16.2.0

License

MIT

Unpacked Size

149 kB

Total Files

36

Last publish

Collaborators

  • angular
  • google-wombot