Node.js C++ 模块开发:.cpp 和 .h 文件的 HelloWorld 示例
以下是一个简单的 Node.js C++ 模块,包含了 .cpp 和 .h 文件的 helloworld 示例:
helloworld.cpp:
#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(helloworld, init)
}
helloworld.h:
#ifndef HELLOWORLD_H
#define HELLOWORLD_H
#include <node.h>
namespace demo {
void Method(const v8::FunctionCallbackInfo<v8::Value>& args);
}
#endif
在 Node.js 应用程序中使用该模块:
const addon = require('./build/Release/helloworld.node');
console.log(addon.hello()); // 'world'
原文地址: https://www.cveoy.top/t/topic/opwp 著作权归作者所有。请勿转载和采集!