4.1.0 • Published 4 months ago

@rowanmanning/response-render-middleware v4.1.0

Weekly downloads
2
License
MIT
Repository
github
Last release
4 months ago

@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

4.1.0

4 months ago

3.0.2

10 months ago

4.0.0

8 months ago

3.0.1

1 year ago

3.0.0

1 year ago

2.1.4

1 year ago

2.1.3

1 year ago

2.1.2

2 years ago

2.1.1

2 years ago

2.1.0

2 years ago

2.0.1

2 years ago

1.1.0

2 years ago

2.0.0

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

3 years ago