You can add options to Express middleware by exporting a function that accepts an options parameter, which then returns the middleware. In hapi, you set the options when you register the plugin. Lets have a look:
Express:
module.exports = function (options) {
return function getDate(req, res, next) {
req.getDate = function() {
const date = 'Hello ' + options.name + ', the date is ' + new Date();
return date;
};
next()
};
};
const getDate = {
name: 'getDate',
version: '1.0.0',
register: async function (server, options) {
const currentDate = function() {
const date = 'Hello ' + options.name + ', the date is ' + new Date();
return date;
};
server.decorate('toolkit', 'getDate', currentDate);
}
};
Fixed:
Options
You can add options to Express middleware by exporting a function that accepts an options parameter, which then returns the middleware. In hapi, you set the options when you register the plugin. Lets have a look:
Express:
module.exports = function (options) {
return function getDate(req, res, next) {
req.getDate = function() {
const date = 'Hello ' + options.name + ', the date is ' + new Date();
return date;
};
next()
};
};
hapi:
const getDate = {
name: 'getDate',
version: '1.0.0',
register: async function (server, options) {
const currentDate = function() {
const date = 'Hello ' + options.name + ', the date is ' + new Date();
return date;
};
server.decorate('toolkit', 'getDate', currentDate);
}
};
To get access to the options in hapi, you simply refer to the options object when you create the plugin:
In Express Migration Tutorial -> Loading a plugin -> Options there was a mix between the express and hapi examples.
Original:
Options
You can add options to Express middleware by exporting a function that accepts an options parameter, which then returns the middleware. In hapi, you set the options when you register the plugin. Lets have a look:
Express:
hapi:
To get access to the options in hapi, you simply refer to the
options
object when you create the plugin:Express:
hapi:
Fixed:
Options
You can add options to Express middleware by exporting a function that accepts an options parameter, which then returns the middleware. In hapi, you set the options when you register the plugin. Lets have a look:
Express:
hapi:
To get access to the options in hapi, you simply refer to the
options
object when you create the plugin:Express:
hapi: