MessageHandler.HandleMessage(message);
The question might seem to be 2 parts, but I think there might be overlap between them.
I'm making a very simple Node application where I could, theoretically, put all my functions in the same JS file. However like C# (where you could theoretically put all your classes on the same page) this is going to get cluttered very quickly and is likely considered bad practice.
I have the basic node generation structure (as shown in the sublime explorer):
But I'm not sure where is the standard place for me to start putting my JS.
Looping back to the first part of my question I have this very simple code.
//file: MyNode.js
const MessageHandler = require('MessageHandler.js');
var message = "Hello world!";
MessageHandler.HandleMessage(message);
And:
//file: MessageHandler.js
function HandleMessage(message)
{
Console.log('Message is this: ' + message);
}
They are in the same file directory but during execution I get a HandleMessage is not a function.
type error. Is there a structuring or scope problem that I'm not seeing?
Actually, your code is correct but needs little addition, you need to export the required function to expose or to use in another js file.
// a.js
anyFun() {
// body
}
you can use module.exports = anyFun;
//b.js
let a = require('./a')
a();
In your case:
//file: MessageHandler.js
module.exports.HandleMessage = function HandleMessage(message)
{
console.log('Message is this: ' + message);
}
//file: MyNode.js
let demo = require("./MessageHandler");
demo.HandleMessage('hello');