filesafe/controllers/middlewares/nunjucksRenderer.js
2022-07-12 06:24:04 +07:00

45 lines
1001 B
JavaScript

const nunjucks = require('nunjucks')
class NunjucksRenderer {
directory
environment
constructor (directory = '', options = {}) {
if (typeof directory !== 'string') {
throw new TypeError('Root directory must be a string value')
}
this.directory = directory
this.environment = nunjucks.configure(
this.directory,
Object.assign(options, {
autoescape: true
})
)
}
#middleware (req, res, next) {
// Inject render() method into Response on each requests
res.render = (path, params) => this.#render(res, path, params)
return next()
}
#render (res, path, params) {
return new Promise((resolve, reject) => {
this.environment.render(`${path}.njk`, params, (err, html) => {
if (err) return reject(err)
resolve(html)
})
}).then(html => {
return res.type('html').send(html)
})
}
get middleware () {
return this.#middleware.bind(this)
}
}
module.exports = NunjucksRenderer