I want to understand how HAP-Node.js works, but I cannot follow the Node.js code. I'm very new to Node.js, but not new to programming.
In this example below (which is the HAP-NodeJS project and can be found on GitHub) I cannot understand how the execution starts. I think the require functions at the top return only the exported object from the modules, but nothing in the require()’ed modules gets executed. Then I also see that the index.js file has nothing that appears to actually execute either. It appears to only define an object and init function that is called from some mystery caller.
As a comparison, in a C or Java program, there is an entry point function Main() that get called to start everything off. How does the “flow” work in this Node.js project? What can I look for to gain understanding of this? Is there some kind of Node.js preprocessor you can put all the code in to see it as a whole?
Please don’t take this question down to recommend I post it on GitHub because I truly think I’m asking a Node.js general question.
var Accessory = require('./lib/Accessory.js').Accessory;
var Bridge = require('./lib/Bridge.js').Bridge;
var Camera = require('./lib/Camera.js').Camera;
var Service = require('./lib/Service.js').Service;
var Characteristic = require('./lib/Characteristic.js').Characteristic;
var uuid = require('./lib/util/uuid');
var AccessoryLoader = require('./lib/AccessoryLoader.js');
var StreamController = require('./lib/StreamController.js').StreamController;
var storage = require('node-persist');
// ensure Characteristic subclasses are defined
var HomeKitTypes = require('./lib/gen/HomeKitTypes');
module.exports = {
init: init,
Accessory: Accessory,
Bridge: Bridge,
Camera: Camera,
Service: Service,
Characteristic: Characteristic,
uuid: uuid,
AccessoryLoader: AccessoryLoader,
StreamController: StreamController
}
function init(storagePath) {
// initialize our underlying storage system, passing on the directory if needed
if (typeof storagePath !== 'undefined')
storage.initSync({ dir: storagePath });
else
storage.initSync(); // use whatever is default
}