Hello World
Let's build our first add-on. In keeping with tradition, this add-on will result in a Node module that will print out Hello World!
. Even though this is a very simple example, it typifies the structure of all further C++ add-ons you will build. This allows you to incrementally experiment with new commands and structures, growing your knowledge in easy to understand steps.
C++ files are typically given a
.cc
extension. To start, we create a hello.cc
file:
#include <node.h> #include <v8.h> using namespace v8; Handle<Value> Method(const Arguments& args) { HandleScope scope; return scope.Close(String::New("Hello World!")); } void init(Handle<Object> target) { target->Set(String::NewSymbol("hello"), FunctionTemplate::New(Method)->GetFunction()); } NODE_MODULE(hello, init)
This example includes the Node and V8 libraries, building our module using the same libraries Node modules build on NODE_MODULE
is simply a...