Skip to content

Instantly share code, notes, and snippets.

@caub
Last active July 20, 2018 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caub/212458096f251d63138d2a76a089b53e to your computer and use it in GitHub Desktop.
Save caub/212458096f251d63138d2a76a089b53e to your computer and use it in GitHub Desktop.
// render .js files as templates (they must be function taking options as arguments and outputting a valid HTML string)
app.engine('js', function (filePath, options, callback) {
if (process.env.NODE_ENV !== 'production') {
require.cache[filePath] = undefined; // invalidate require cache in dev
}
callback(null, require(filePath)(options)); // note: should wrap in a safeHtml in prod
});
app.set('views', __dirname + '/views');
app.set('view engine', 'js');
// VS
app.engine('js', reactEngine);
function reactEngine(filePath, options, callback) {
const component = require(filePath);
const markup = renderToStaticMarkup(createElement(component, options));
callback(null, markup);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment