Simple express function to expose the contents
of a package.json file to the view engine. This is useful for displaying
the software version on a web page without having to update the page every
time the version changes in package.json.
npm install --save express-package-json
Parameters:
pathToPackageJson- path to thepackage.jsonfile. String. Defaults to./package.json.propertyName- name of property to add tores.locals. String. Defaults topkg.
Returns:
- express middleware function:
- middleware function accepts
(req, res, next). res.locals[variableName]is set to a JavaScript object containing the parsedpackage.json.next()is called when complete.
- middleware function accepts
app.js:
"use strict";
var express = require('express');
var expressPackageJson = require('express-package-json');
var app = express();
app.set('view engine', 'hbs');
app.use(expressPackageJson(path.join(__dirname, 'package.json')));
app.get('/', function (req, res) {
res.render('index');
});
views/index.hbs:
{{pkg.name}} v{{pkg.version}}
npm test
See LICENSE.md