This package has been deprecated

Author message:

This module is no longer maintained.

@rowanmanning/hijack-express-render
TypeScript icon, indicating that this package has built-in type declarations

4.0.0 • Public • Published

@rowanmanning/hijack-express-render

Override an Express (4.x) application's render methods. This function replaces the app.render and response.render methods of an Express application, allowing you to bypass Express view rendering entirely.

This allows you to implement your own template resolution and caching, but retain the standard Express API.

⚠️ This is probably a bad idea, and prone to breaking if Express changes the way it renders things

Table of Contents

Requirements

This library requires the following to run:

Usage

Install with npm:

npm install @rowanmanning/hijack-express-render

Load the library into your code with a require call:

const hijackExpressRender = require('@rowanmanning/hijack-express-render');

Create an Express application and call hijackExpressRender with this and a new render method (see Render Methods below):

const express = require('express');
const hijackExpressRender = require('@rowanmanning/hijack-express-render');

const app = express();
hijackExpressRender(app, yourRenderMethod);

The hijackExpressRender also returns the Express application, so you can do this in one line:

const app = hijackExpressRender(express(), yourRenderMethod);

Hijacking of the render methods must happen before any middleware that uses app.render or response.render. It's safest to hijack immediately after creating the Express application.

Render Methods

The render method you can pass into hijackExpressRender must:

  1. Accept two parameters…
    1. view: The name of the view to render (normally a string). This will not be relative to the Express "view" directory – it is exactly what was passed into app.render or response.render. Your method must handle its own template resolution and caching. You may also use a non-string here if your renderer supports this
    2. context: The render context (as an object). This will be defaulted using app.locals and response.locals in the same way as the default Express rendering engine
  2. Return a Promise which resolves with the rendered template. This is normally a string but can be any type that's accepted by response.send

Example with a Promise:

function renderMethod(view, context) {
    return Promise.resolve('result');
}

Example with an async function:

async function renderMethod(view, context) {
    return 'result';
}

Contributing

The contributing guide is available here. All contributors must follow this library's code of conduct.

License

Licensed under the MIT license.
Copyright © 2019, Rowan Manning

Readme

Keywords

Package Sidebar

Install

npm i @rowanmanning/hijack-express-render

Weekly Downloads

1

Version

4.0.0

License

MIT

Unpacked Size

9.42 kB

Total Files

7

Last publish

Collaborators

  • rowanmanning