Skip to content

Instantly share code, notes, and snippets.

@betomoretti
Last active January 17, 2018 18:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save betomoretti/90788e123bf8a55118bebddac5024fda to your computer and use it in GitHub Desktop.
Save betomoretti/90788e123bf8a55118bebddac5024fda to your computer and use it in GitHub Desktop.
Hello world example using a native extension implemented with NAPI
{
"targets": [
{
"cflags!": [ "-fno-exceptions" ],
"cflags_cc!": [ "-fno-exceptions" ],
"include_dirs" : [
"<!@(node -p \"require('node-addon-api').include\")"
],
"target_name": "hello_world",
"sources": [ "hello_world.cc" ],
'defines': [ 'NAPI_DISABLE_CPP_EXCEPTIONS' ]
}
]
}
#include <napi.h>
Napi::String SayHi(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
return Napi::String::New(env, "Hi!");
}
Napi::Object init(Napi::Env env, Napi::Object exports) {
exports.Set(Napi::String::New(env, "sayHi"), Napi::Function::New(env, SayHi));
return exports;
};
NODE_API_MODULE(hello_world, init);
const hello_world = require('bindings')('hello_world')
console.log(hello_world.sayHi());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment