Move point-of-view setup to separate file

This commit is contained in:
Robbie Antenesse 2019-09-06 23:12:42 -06:00
parent 0bbfafd03f
commit 621ee61144
2 changed files with 32 additions and 21 deletions

View File

@ -21,27 +21,7 @@ fastify.register(require('fastify-formbody')); // Enable fastify to parse data
fastify.register(require('fastify-static'), { // Enable delivering static content efficiently fastify.register(require('fastify-static'), { // Enable delivering static content efficiently
root: path.join(__dirname, 'public'), // all static content will be delivered from the public/ folder root: path.join(__dirname, 'public'), // all static content will be delivered from the public/ folder
}); });
fastify.register(require('point-of-view'), { // Adds the `view()` function to fastify's `reply` objects fastify.register(require('point-of-view'), require('./views/viewSetup')); // Adds the `view()` function to fastify's `reply` objects
engine: {
handlebars: require('handlebars'), // Use handlebar as the render engine for `reply.view()`
},
templates: 'views', // Search for all files referenced in `reply.view()` within the `views/` folder
options: {
useHtmlMinifier: require('html-minifier'), // Add a minifier to the rendered HTML output
htmlMinifierOptions: {
removeComments: true,
removeCommentsFromCDATA: true,
collapseWhitespace: true,
collapseBooleanAttributes: true,
removeEmptyAttributes: true
},
partials: { // Specifies the Handlebars Partials so `point-of-view` knows where they are within the `views` folder and what they're called when referenced in a `.hbs` file
layout: 'layout.hbs',
header: 'partials/header.hbs',
footer: 'partials/footer.hbs',
}
},
});
fastify.register(require('fastify-cookie')); // Enable reading and setting http-level cookies for the sole purpose of storing login tokens fastify.register(require('fastify-cookie')); // Enable reading and setting http-level cookies for the sole purpose of storing login tokens
fastify.register(require('fastify-jwt'), { // Enable creating, parsing, and verifying JSON Web Tokens from the global fastify object fastify.register(require('fastify-jwt'), { // Enable creating, parsing, and verifying JSON Web Tokens from the global fastify object
secret: fastify.siteConfig.jwtSecretKey, // The secret key used to generate JWTs. Make it big and random! secret: fastify.siteConfig.jwtSecretKey, // The secret key used to generate JWTs. Make it big and random!

31
views/viewSetup.js Normal file
View File

@ -0,0 +1,31 @@
const fs = require('fs');
const path = require('path');
const partialFiles = fs.readdirSync(path.resolve(__dirname, 'partials')); // This is resolved from *this file's* dirname
const partials = {
layout: 'layout.hbs',
};
partialFiles.forEach(file => {
if (file.includes('.hbs')) {
const name = file.replace('.hbs', '');
partials[name] = `partials/${file}`;
}
});
module.exports = { // Adds the `view()` function to fastify's `reply` objects
engine: {
handlebars: require('handlebars'), // Use handlebar as the render engine for `reply.view()`
},
templates: 'views', // Search for all files referenced in `reply.view()` within the `views/` folder
options: {
useHtmlMinifier: require('html-minifier'), // Add a minifier to the rendered HTML output
htmlMinifierOptions: {
removeComments: true,
removeCommentsFromCDATA: true,
collapseWhitespace: true,
collapseBooleanAttributes: true,
removeEmptyAttributes: true
},
partials, // Specifies the Handlebars Partials so `point-of-view` knows where they are within the `views` folder and what they're called when referenced in a `.hbs` file
},
}