BLOG
Comment créer son premier addons NodeJS
1- Installation de l’environnement
1.1 Installation de nodejs
$ sudo apt-get install python git-core pkg-config build-essential $ wget http://nodejs.org/dist/v0.10.21/node-v0.10.21.tar.gz $ tar xvzf node-v0.10.21.tar.gz && cd node-v0.10.21 $ ./configure && make && sudo make install
1.2 Installation de node-gyp
node-gyp un outil qui va vous permettre de compiler vos projets nodejs
$ sudo npm install -g node-gyp
2- Hello world
2.1 Création du projet
$ mkdir -p node-hello/src $ git init $ vim .gitignore node_modules build
2.2 Le code en lui même
On va pouvoir commencer à écrire un peu de code à présent 🙂
$ vim src/hello.cc #include #include using namespace v8; Handle Method(const Arguments& args) { HandleScope scope; return scope.Close(String::New("world")); } void init(Handle
2.3 Le fichier binding.gyp
{ "targets": [ { "target_name": "hello", "sources": [ "src/hello.cc" ] } ] }
2.4 On compile et on test
$ node-gyp configure build $vim hello.js var addon = require('./build/Release/hello'); console.log(addon.hello()); // 'world'
3- Sources:
- L’API de nodeJS: http://nodejs.org/api/addons.html.
- Le code source: https://github.com/ldandoy/node-hello.