function myfunc() {
// some codes...
}
exports = myfunc;
When nodejs addon is above type, I can use it as ...
var myfunc = require("./myfunc");
myfunc();
How can I make this type addon in c++?
You can set myfunc as the exports object by setting module.exports directly:
function myfunc() {
}
module.exports = myfunc;
The Modules documentation covers the distinction between exports and module.exports:
Note that
exportsis a reference tomodule.exportsmaking it suitable for augmentation only. If you are exporting a single item such as a constructor you will want to usemodule.exportsdirectly instead.function MyConstructor (opts) { //... } // BROKEN: Does not modify exports exports = MyConstructor; // exports the constructor properly module.exports = MyConstructor;
As for making it a C++ Addon, a rough example would be:
#include <node.h>
using namespace v8;
Handle<Value> MyFunc(const Arguments& args) {
HandleScope scope;
return scope.Close(Undefined());
}
void Init(Handle<Object> exports, Handle<Object> module) {
module->Set(
String::NewSymbol("exports"),
FunctionTemplate::New(MyFunc)->GetFunction()
);
}
NODE_MODULE(target_name, Init);
To build it, you'll need node-gyp, its dependencies, and a binding.gyp.
And, note that the 1st argument for NODE_MODULE() should match the value of "target_name" in the binding.gyp.
Then:
node-gyp rebuild