Spaces:
Sleeping
Sleeping
Commit ·
dc21909
1
Parent(s): 5ae4842
add napi
Browse files
app_cppnode/app_cppnode_addon/app_cppnode_addon.cpp
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#include <napi.h>
|
| 2 |
+
#include <string>
|
| 3 |
+
|
| 4 |
+
// 示例 N-API 函数
|
| 5 |
+
Napi::String Hello(const Napi::CallbackInfo& info) {
|
| 6 |
+
Napi::Env env = info.Env();
|
| 7 |
+
|
| 8 |
+
std::string name = "World";
|
| 9 |
+
if (info.Length() > 0 && info[0].IsString()) {
|
| 10 |
+
name = info[0].As<Napi::String>().Utf8Value();
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
std::string result = "Hello, " + name + " from N-API!";
|
| 14 |
+
return Napi::String::New(env, result);
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
// 计算函数示例
|
| 18 |
+
Napi::Number Add(const Napi::CallbackInfo& info) {
|
| 19 |
+
Napi::Env env = info.Env();
|
| 20 |
+
|
| 21 |
+
if (info.Length() < 2) {
|
| 22 |
+
Napi::TypeError::New(env, "需要两个参数").ThrowAsJavaScriptException();
|
| 23 |
+
return Napi::Number::New(env, 0);
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
if (!info[0].IsNumber() || !info[1].IsNumber()) {
|
| 27 |
+
Napi::TypeError::New(env, "参数必须是数字").ThrowAsJavaScriptException();
|
| 28 |
+
return Napi::Number::New(env, 0);
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
double a = info[0].As<Napi::Number>().DoubleValue();
|
| 32 |
+
double b = info[1].As<Napi::Number>().DoubleValue();
|
| 33 |
+
|
| 34 |
+
return Napi::Number::New(env, a + b);
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
// 初始化函数
|
| 38 |
+
Napi::Object Init(Napi::Env env, Napi::Object exports) {
|
| 39 |
+
exports.Set(Napi::String::New(env, "hello"),
|
| 40 |
+
Napi::Function::New(env, Hello));
|
| 41 |
+
exports.Set(Napi::String::New(env, "add"),
|
| 42 |
+
Napi::Function::New(env, Add));
|
| 43 |
+
return exports;
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
NODE_API_MODULE(addon, Init)
|
app_cppnode/app_cppnode_addon/binding.gyp
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"targets": [
|
| 3 |
+
{
|
| 4 |
+
"target_name": "app_cppnode_addon",
|
| 5 |
+
"sources": ["app_cppnode_addon.cpp"],
|
| 6 |
+
"include_dirs": [
|
| 7 |
+
"<!(node -e \"require('node-addon-api').include\")"
|
| 8 |
+
],
|
| 9 |
+
"dependencies": ["<!(node -e \"require('node-addon-api').gyp\")"],
|
| 10 |
+
"cflags!": ["-fno-exceptions"],
|
| 11 |
+
"cflags_cc!": ["-fno-exceptions"],
|
| 12 |
+
"defines": ["NAPI_DISABLE_CPP_EXCEPTIONS"],
|
| 13 |
+
"conditions": [
|
| 14 |
+
["OS=='win'", {
|
| 15 |
+
"defines": ["_HAS_EXCEPTIONS=1"]
|
| 16 |
+
}]
|
| 17 |
+
]
|
| 18 |
+
}
|
| 19 |
+
]
|
| 20 |
+
}
|
app_cppnode/app_cppnode_addon/index.js
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const app_node_addon = require('./build/Release/app_node_addon.node');
|
| 2 |
+
|
| 3 |
+
module.exports = app_node_addon;
|
app_cppnode/app_cppnode_addon/package.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "app_cppnode_addon",
|
| 3 |
+
"version": "1.0.0",
|
| 4 |
+
"description": "N-API Addon示例",
|
| 5 |
+
"main": "index.js",
|
| 6 |
+
"gypfile": true,
|
| 7 |
+
"scripts": {
|
| 8 |
+
"build": "node-gyp rebuild",
|
| 9 |
+
"clean": "node-gyp clean"
|
| 10 |
+
},
|
| 11 |
+
"dependencies": {
|
| 12 |
+
"node-addon-api": "^5.0.0"
|
| 13 |
+
},
|
| 14 |
+
"devDependencies": {
|
| 15 |
+
"node-gyp": "^9.0.0"
|
| 16 |
+
}
|
| 17 |
+
}
|
app_cppnode/server.js
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const express = require('express');
|
| 2 |
+
const addon = require('./app_node_addon');
|
| 3 |
+
|
| 4 |
+
const app = express();
|
| 5 |
+
const PORT = process.env.PORT || 3000;
|
| 6 |
+
|
| 7 |
+
app.use(express.json());
|
| 8 |
+
|
| 9 |
+
// 健康检查
|
| 10 |
+
app.get('/health', (req, res) => {
|
| 11 |
+
res.json({ status: 'ok', service: 'node-napi' });
|
| 12 |
+
});
|
| 13 |
+
|
| 14 |
+
// 调用 N-API addon
|
| 15 |
+
app.post('/api/napi/hello', (req, res) => {
|
| 16 |
+
try {
|
| 17 |
+
const { name } = req.body;
|
| 18 |
+
const result = addon.hello(name || 'World');
|
| 19 |
+
res.json({ success: true, result });
|
| 20 |
+
} catch (error) {
|
| 21 |
+
res.status(500).json({ success: false, error: error.message });
|
| 22 |
+
}
|
| 23 |
+
});
|
| 24 |
+
|
| 25 |
+
// 调用 N-API addon 计算
|
| 26 |
+
app.post('/api/napi/add', (req, res) => {
|
| 27 |
+
try {
|
| 28 |
+
const { a, b } = req.body;
|
| 29 |
+
if (typeof a !== 'number' || typeof b !== 'number') {
|
| 30 |
+
return res.status(400).json({
|
| 31 |
+
success: false,
|
| 32 |
+
error: '参数必须是数字'
|
| 33 |
+
});
|
| 34 |
+
}
|
| 35 |
+
const result = addon.add(a, b);
|
| 36 |
+
res.json({ success: true, result });
|
| 37 |
+
} catch (error) {
|
| 38 |
+
res.status(500).json({ success: false, error: error.message });
|
| 39 |
+
}
|
| 40 |
+
});
|
| 41 |
+
|
| 42 |
+
// 复杂计算示例
|
| 43 |
+
app.post('/api/napi/compute', (req, res) => {
|
| 44 |
+
try {
|
| 45 |
+
const { numbers, operation } = req.body;
|
| 46 |
+
|
| 47 |
+
if (!Array.isArray(numbers)) {
|
| 48 |
+
return res.status(400).json({
|
| 49 |
+
success: false,
|
| 50 |
+
error: 'numbers 必须是数组'
|
| 51 |
+
});
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
let result;
|
| 55 |
+
switch (operation) {
|
| 56 |
+
case 'sum':
|
| 57 |
+
result = numbers.reduce((acc, num) => acc + num, 0);
|
| 58 |
+
break;
|
| 59 |
+
case 'average':
|
| 60 |
+
result = numbers.reduce((acc, num) => acc + num, 0) / numbers.length;
|
| 61 |
+
break;
|
| 62 |
+
default:
|
| 63 |
+
return res.status(400).json({
|
| 64 |
+
success: false,
|
| 65 |
+
error: '不支持的运算类型'
|
| 66 |
+
});
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
res.json({ success: true, result });
|
| 70 |
+
} catch (error) {
|
| 71 |
+
res.status(500).json({ success: false, error: error.message });
|
| 72 |
+
}
|
| 73 |
+
});
|
| 74 |
+
|
| 75 |
+
app.listen(PORT, () => {
|
| 76 |
+
console.log(`Node.js N-API 服务运行在端口 ${PORT}`);
|
| 77 |
+
});
|