javascript - NodeJS require a function inside module exports? -


i have created function register new partial handlebars. don't want have stuffed inside same file, using module exports export function.

home.js - used render views

var fs = require('fs'); var handlebars = require('hbs');  // directories, files , other variables var viewsdir = __dirname + '/../views';  // function helpers var renderhtml = require('./helpers/render_html')(fs, viewsdir);  exports.index = function(req, res) {      renderhtml('main', 'head');     renderhtml('main', 'body');      res.render('layout', {         blogtitle: "my website",         blogdescription: "hello! description :)"     }); }; 

render_html.js - function register handlebars partial

module.exports = function(fs, viewsdir) {     var renderhtml = function(file, section) {         var newsection, handlebarstemplate;          if (typeof section === undefined) {             newsection = "htmlbody";         } else if (section === "body") {             newsection = "htmlhead";         } else if (section === "head") {             newsection = "htmlbody";         }          handlebarstemplate = fs.readfilesync(viewsdir + '/' + file + '.html', 'utf8');         handlebars.registerpartial(newsection, handlebarstemplate);     }; }; 

whenever call "renderhtml()" throws error function undefined. how proceed?

you never returned renderhtml method. try this:

module.exports = function(fs, viewsdir) {     var renderhtml = function(file, section) {         var newsection, handlebarstemplate;         if (typeof section === undefined) {             newsection = "htmlbody";         } else if (section === "body") {             newsection = "htmlhead";         } else if (section === "head") {             newsection = "htmlbody";         }         handlebarstemplate = fs.readfilesync(viewsdir + '/' + file + '.html', 'utf8');         handlebars.registerpartial(newsection, handlebarstemplate);     };     return renderhtml; }; 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -