I use Express-Handlebars and want to refactor this code example to separate files
const express = require('express');
const exphbs = require('express-handlebars');
const handlebars = exphbs.create({
defaultLayout: 'index',
extname: 'hbs',
helpers: {
foo: function () { // first helper
return 'FOO!';
},
bar: function () { // second helper
return 'BAR!';
}
//, nth helper ...
}
});
The reason is why would you place all the HTML logic into your app.js
file. I want to have 1 file for 1 helper.
How can I register the helpers from external files? Could someone provide me an example?
Try to create one module per helper for example inside helpers
folder:
helpers/foo.js:
var foo = function () {
return 'FOO!';
}
module.exports = foo;
helpers/bar.js:
var bar = function () {
return 'BAR!';
}
module.exports = bar;
app.js:
const express = require('express');
const exphbs = require('express-handlebars');
const foo = require('helpers/foo');
const bar = require('helpers/bar');
const handlebars = exphbs.create({
defaultLayout: 'index',
extname: 'hbs',
helpers: {
foo: foo,
bar: bar
}
});