# @nguniversal/express-engine

> Express Engine for running Server Angular Apps

Latest version **16.2.0** (published 2023-08-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install @nguniversal/express-engine
pnpm add @nguniversal/express-engine
yarn add @nguniversal/express-engine
bun add @nguniversal/express-engine
```

## Health

**Score 0/100 (F)** — status: abandoned.

Positive: has types; esm support.

Warnings: low downloads; low quality score.

Negative: critical vulnerabilities; abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 16.2.0 |
| Published | 2023-08-11 |
| First published | 2017-04-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Node | ^16.14.0 \|\| >=18.10.0 |
| Dependencies | 2 |
| Unpacked size | 145.5 KB |
| Known vulnerabilities | 1 (+2 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 3991 |
| Maintainers | angular, google-wombot |
| Keywords | express, ssr, universal |

## Links

- npm: https://www.npmjs.com/package/@nguniversal/express-engine
- Repository: https://github.com/angular/universal
- Issues: https://github.com/angular/universal/issues
- npm.io page: https://npm.io/package/@nguniversal/express-engine

## Dependencies (2)

- [tslib](https://npm.io/package/tslib.md) ^2.3.0
- [@nguniversal/common](https://npm.io/package/@nguniversal/common.md) 16.2.0

## Recent versions

- 16.2.0 (latest) — 2023-08-11
- 16.2.0-rc.0 (next) — 2023-08-04
- 15.2.1 (v15-lts) — 2023-04-13
- 14.2.3 (v14-lts) — 2022-11-23
- 13.1.1 (v13-lts) — 2022-05-04
- 16.1.3 — 2023-08-09
- 16.1.2 — 2023-08-04
- 16.1.1 — 2023-06-28
- 16.1.0 — 2023-06-13
- 16.1.0-rc.0 — 2023-06-07
- 16.1.0-next.0 — 2023-05-19
- 16.0.2 — 2023-05-17
- 16.0.1 — 2023-05-11
- 16.0.0 — 2023-05-03
- 16.0.0-rc.2 — 2023-04-28
- … 124 more at https://npm.io/package/@nguniversal/express-engine/versions

## README

# 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

```ts
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:

```ts
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

```ts
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

```ts
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

```ts
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

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

---
_Source: https://npm.io/package/@nguniversal/express-engine · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
