@rowanmanning/response-render-middleware
v4.2.1
Published
A small convenience utility to generate Express middleware which renders a view
Downloads
5
Maintainers
Readme
@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:
- Node.js 18+
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