forked from nodejscn/node-api-cn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hello_world.md
49 lines (33 loc) · 1.11 KB
/
hello_world.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
“Hello World” 示例是一个简单的插件,用 C++ 编写,相当于以下 JavaScript 代码:
```js
module.exports.hello = () => 'world';
```
首先,创建 `hello.cc` 文件:
```cpp
// hello.cc
#include <node.h>
namespace demo {
using v8::FunctionCallbackInfo;
using v8::Isolate;
using v8::Local;
using v8::Object;
using v8::String;
using v8::Value;
void Method(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
args.GetReturnValue().Set(String::NewFromUtf8(isolate, "world"));
}
void init(Local<Object> exports) {
NODE_SET_METHOD(exports, "hello", Method);
}
NODE_MODULE(NODE_GYP_MODULE_NAME, Init)
} // namespace demo
```
注意,所有的 Node.js 插件必须导出一个如下模式的初始化函数:
```cpp
void Initialize(Local<Object> exports);
NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
```
`NODE_MODULE` 后面没有分号,因为它不是一个函数(详见 `node.h`)。
`module_name` 必须匹配最终的二进制文件名(不包括 .node 后缀)。
在 `hello.cc` 示例中,初始化函数是 `init`,插件模块名是 `addon`。