From 960ccdc72cefa9c7b9e588a1d149a1dba7a0c296 Mon Sep 17 00:00:00 2001 From: Artur Adib Date: Tue, 8 Nov 2011 21:13:59 -0500 Subject: [PATCH] docs: clarify addon docs --- doc/api/addons.markdown | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/doc/api/addons.markdown b/doc/api/addons.markdown index 3234e7a60..3e815d4d4 100644 --- a/doc/api/addons.markdown +++ b/doc/api/addons.markdown @@ -22,8 +22,8 @@ Node statically compiles all its dependencies into the executable. When compiling your module, you don't need to worry about linking to any of these libraries. -To get started let's make a small Addon which does the following except in -C++: +To get started let's make a small Addon which is the C++ equivalent of +the following Javascript code: exports.hello = function() { return 'world'; }; @@ -40,11 +40,18 @@ To get started we create a file `hello.cc`: } void init(Handle target) { - NODE_SET_METHOD(target, "method", Method); + NODE_SET_METHOD(target, "hello", Method); } NODE_MODULE(hello, init) -This source code needs to be built into `hello.node`, the binary Addon. To +Note that all Node addons must export an initialization function: + + void Initialize (Handle target); + NODE_MODULE(module_name, Initialize) + +There is no semi-colon after `NODE_MODULE` as it's not a function (see `node.h`). + +The source code needs to be built into `hello.node`, the binary Addon. To do this we create a file called `wscript` which is python code and looks like this: @@ -70,10 +77,12 @@ Running `node-waf configure build` will create a file `node-waf` is just [WAF](http://code.google.com/p/waf), the python-based build system. `node-waf` is provided for the ease of users. -All Node addons must export an initialization function: +You can now use the binary addon in a Node project `hello.js` by pointing `require` to +the recently built module: - void Initialize (Handle target); - NODE_MODULE(hello, Initialize) + var addon = require('./build/Release/hello'); + + console.log(addon.hello()); // 'world' For the moment, that is all the documentation on addons. Please see for a real example. -- 2.34.1