nodejs: C++扩展
来源:互联网 发布:轩辕剑入灵进阶数据 编辑:程序博客网 时间:2024/06/08 02:51
Nodejs的C++扩展
首先保证nodejs和v8都正确安装
首先保证nodejs和v8都正确安装
下载NodeJS源码,我的放在D盘。
NodeJS的C++扩展要用VS2010开发,新建一个空的Win32控制台项目,右键——属性,在常规中将目标文件扩展名改为.node
在C/C++,常规中,在附加包含目录中添加NodeJS包含目录 ,D:\nodejs\include
在连接器——常规中的附加库目录中添加NodeJS的lib库: D:\nodejs\lib
在输入中添加附加库依赖项:node.lib
配置完毕,就可以就行扩展开发了。
新建hello.cpp
1 #include <node.h> 2 3 #include <string> 4 5 6 7 using namespace std; 8 9 using namespace v8;10 11 12 13 Handle<Value> Hello(const Arguments& args)14 15 {16 17 HandleScope scope;18 19 return scope.Close(String::New("Hello world!"));20 21 }22 23 24 25 Handle<Value> Add(const Arguments& args)26 27 {28 29 HandleScope scope;30 31 32 33 if (args.Length() < 2)34 35 {36 37 ThrowException(Exception::TypeError(String::New("Wrong number of arguments")));38 39 return scope.Close(Undefined());40 41 }42 43 44 45 if (!args[0]->IsNumber() || !args[1]->IsNumber())46 47 {48 49 ThrowException(Exception::TypeError(String::New("Wrong arguments")));50 51 return scope.Close(Undefined());52 53 }54 55 56 57 Local<Number> num = Number::New(args[0]->NumberValue() +58 59 args[1]->NumberValue());60 61 return scope.Close(num);62 63 }64 65 66 67 extern "C"68 69 {70 71 void init(Handle<Object> target)72 73 {74 75 NODE_SET_METHOD(target, "hello", Hello); //对外输出hello方法76 77 NODE_SET_METHOD(target, "add", Add);78 79 }80 81 //输出的扩展类名hello82 83 NODE_MODULE(hello, init)84 85 }
执行命令
编译
会在当前目录生成Release/hello.node
编写nodejs脚本hello.js
1 var cpphello = require('./Release/hello');2 3 console.log(hello.hello()); // hello world4 5 console.log(hello.add (2,3)); // 5
执行命令
按照前面文章的提示,在node目录下,执行命令行
>node hello.js
即可看到输出
hello world!
5
0 0
- NodeJS C/C++扩展之Hello World~
- nodejs: C++扩展
- C++编写nodejs扩展实战
- NodeJS开发c++扩展模块
- C 扩展
- c 扩展
- Nodejs扩展,实现消息弹窗
- c++写nodejs的扩展模块
- nodejs c++扩展之hello world
- Electron如何调用NodeJS扩展模块
- Electron使用NodeJS扩展模块调用DLL
- webstorm中使用nodejs 扩展 supervisor
- nodejs修改ejs扩展名为html
- nodejs调用c++dll感想
- zeromq:c,c++,golang及nodejs使用
- nodejs扩展log4js正则匹配区分文件(方案一)
- windows平台搭带c++扩展的nodejs环境
- 扩展C++, Java, C#
- nodejs:入门
- 多态与继承中多态
- nginx启动、重启、关闭、升级
- 无法将网络更改为桥接状态:没有未桥接的主机网络适配器
- 如何不使用代理网站,用谷歌浏览器访问外网
- nodejs: C++扩展
- nodejs:csv模块解析
- nodejs: 理解Buffer
- 一份快速实用的 tcpdump 命令参考手册
- 在线Apache重写规则转Nginx
- 深入浅出LinkedList与ArrayList(2)
- java环境配置总结
- Sed命令基础操作
- nodejs:express 4.x