Skip to content

A small convenience utility to generate Express middleware which renders a view.

License

Notifications You must be signed in to change notification settings

rowanmanning/response-render-middleware

Repository files navigation

@rowanmanning/response-render-middleware

A small convenience utility to generate Express middleware which renders a view.

app.get('/', render('home'));

Table of Contents

Requirements

This library requires the following to run:

Usage

Install with npm:

npm install @rowanmanning/response-render-middleware

Load the library into your code with a require call:

const render = require('@rowanmanning/response-render-middleware');

Use it to generate middleware to render a view:

const app = express();

app.get('/', render('home'));

The following are equivalent:

app.get('/', render('home'));
app.get('/', (request, response) => response.render('home'));
app.get('/blog', render('blog', {title: 'My Blog'}));
app.get('/blog', (request, response) => response.render('blog', {title: 'My Blog'}));

If you need to pass dynamic information into the view, either don't use this middleware, or make use of response.locals in a previous middleware:

function getPostById(request, response, next) {
    response.locals.post = await getPostFromDatabase(request.params.postId);
    next();
}

app.get('/blog/:postId', [
    getPostById,
    render('post')
]);

Contributing

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

License

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

About

A small convenience utility to generate Express middleware which renders a view.

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •