javascript - Calling a Node.js module function from within the module -
i'm attempting write node module in order clean code , separate out different files.
consider below code:
module.exports = { hello : function(request, reply) { return reply("hello " + world()); }, world : function() { return "world"; } }
if import above module , use hello function handler specific route, http 500 internal server error.
i've narrowed problem down call world(), if change hello function to
hello : function(request, reply) { return reply("hello world"); }
then works fine, seems being tripped when calling function within export object
does know why happening , how resolve it?
you should call follows:
module.exports = { hello: function(request, reply) { return reply("hello " + module.exports.world()); }, world: function() { return "world"; } }
if aiming cleaner code, suggest change code this:
function world() { return "world"; } function hello(request, reply) { return reply("hello " + world()); } module.exports = { hello, }
this make code more readable , exporting need. question has other solutions issue.
Comments
Post a Comment