Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
docs: clarify addon docs
Browse files Browse the repository at this point in the history
  • Loading branch information
arturadib authored and bnoordhuis committed Nov 17, 2011
1 parent 3e696d1 commit 960ccdc
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions doc/api/addons.markdown
Expand Up @@ -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'; };

Expand All @@ -40,11 +40,18 @@ To get started we create a file `hello.cc`:
}

void init(Handle<Object> 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<Object> 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:

Expand All @@ -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<Object> 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
<https://github.com/ry/node_postgres> for a real example.

0 comments on commit 960ccdc

Please sign in to comment.