Skip to content
Branch: master
Find file History
Permalink
Type Name Latest commit message Commit time
..
Failed to load latest commit information.
schematics feat(hapi-engine): add ModuleMapLoaderModule to the app server import… May 28, 2019
spec test: re-enable unit tests (#1157) Apr 26, 2019
src test: add tests to ensure that document.location is populated (#1112) Dec 25, 2018
testing test: re-enable unit tests (#1157) Apr 26, 2019
tokens build: update to latest bazel and angular (#1155) Apr 25, 2019
BUILD.bazel test: re-enable unit tests (#1157) Apr 26, 2019
README.md feat(hapi): upgrade to Hapi v17 (#1015) Jun 10, 2018
index.ts
package.json feat(hapi-engine): add schematics (#1057) Dec 25, 2018
public_api.ts build: add secondary entry points for tokens (#922) Apr 4, 2018

README.md

Angular Hapi Engine

This is a Hapi Engine for running Angular Apps on the server for server side rendering.

Usage

npm install @nguniversal/hapi-engine --save

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

import { Request, Server } from 'hapi';
import { ngHapiEngine } from '@nguniversal/hapi-engine';

const server = new Server();
server.connection({
  host: 'localhost',
  port: 8000
});

server.route({
  method: 'GET',
  path: '/{path*}',
  handler: (req: Request) => ngHapiEngine({req, bootstrap: ServerAppModule})
});

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:

server.route({
  method: 'GET',
  path: '/{path*}',
  handler: (req: Request) => {
    const url = 'http://someurl.com';
    const document = '<html><head><title>New doc</title></head></html>';
    return ngHapiEngine({
      req,
      url,
      document,
    });
  }
});

Extra Providers

Extra Providers can be provided either on engine setup

const hapiEngine = ngHapiEngine({
  bootstrap: ServerAppModule,
  providers: [
    ServerService
  ]
});

Advanced Usage

Request based Bootstrap

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

server.route({
  method: 'GET',
  path: '/{path*}',
  handler: (req: Request) => 
    ngHapiEngine({
      bootstrap: OtherServerAppModule,
      providers: [
        OtherServerService
      ],
      req
    })
});

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 'hapi';
import { REQUEST } from '@nguniversal/hapi-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.

You can’t perform that action at this time.