1、在Windows中安装Node.js
1)首先安装VISUAL C++ 2010 EXPRESS(有Visual Studio 2010也可),从:http://www.microsoft.com/visualstudio/en-us/products/2010-editions/visual-cpp-express下载安装即可,注册一个hotmail邮箱即可激活VC++。
2)从http://www.nodejs.org/下载Source Code版本node-v0.6.14.tar.gz,解压到Windows任意目录下,如D:\node-v0.6.14。
3)打开Windows命令行cmd.exe,进入D:\node-v0.6.14,执行vcbuild.bat release,然后经历了漫长的等待后,在Release目录下可以找到编译好的node.exe文件。
4)制作编译安装批处理文件nodins.bat:
[@echo](/user/echo) off
if "%1"=="" goto help
mkdir "%1"
mkdir "%1"\include
copy /y src\node.h "%1"\include
copy /y src\node_object_wrap.h "%1"\include
copy /y src\node_buffer.h "%1"\include
copy /y src\node_version.h "%1"\include
copy /y deps\v8\include\*.h "%1"\include\
copy /y deps\uv\include\*.h "%1"\include\
mkdir "%1"\include\uv-private
copy /y deps\uv\include\uv-private\*.h "%1"\include\uv-private
mkdir "%1"\include\ev
copy /y deps\uv\src\ev\*.h "%1"\include\ev
mkdir "%1"\include\c-ares
copy /y deps\uv\include\ares.h "%1"\include\c-ares
copy /y deps\uv\include\ares_version.h "%1"\include\c-ares
mkdir "%1"\lib
copy /y Release\node.lib "%1"\lib
copy /y Release\node.exe "%1"
echo =================================
echo Install succeefully!
goto exit
if not errorlevel 0 echo Error "install-path" & goto exit
:help
echo nodins.bat install-path
:exit
5)将文件nodins.bat拷贝到D:\node-v0.6.14中,在命令行执行它:nodins.bat D:\nodejs,则生成编译C/C++扩展的编译环境(包括头文件、库和可执行文件)D:\nodejs;
6)在Windows中添加路径环境变量D:\nodejs,主要为了运行node.exe的方便;
2、添加C/C++原生扩展
1)用VISUAL C++ 2010 EXPRESS创建一个DLL工程空白工程hellonode放在D:\;
2)创建一个C++文件如hellonode.cc,贴入一下代码:
#define BUILDING_NODE_EXTENSION
#include <node.h>
using namespace v8;
Handle<Value> Hello(const Arguments& args) {
HandleScope scope;
return scope.Close(String::New("Hello world!"));
}
Handle<Value> Add(const Arguments& args) {
HandleScope scope;
if (args.Length() < 2) {
ThrowException(Exception::TypeError(String::New("Wrong number of arguments")));
return scope.Close(Undefined());
}
if (!args[0]->IsNumber() || !args[1]->IsNumber()) {
ThrowException(Exception::TypeError(String::New("Wrong arguments")));
return scope.Close(Undefined());
}
Local<Number> num = Number::New(args[0]->NumberValue() +
args[1]->NumberValue());
return scope.Close(num);
}
void init(Handle<Object> target) {
NODE_SET_METHOD(target, "hello", Hello);
NODE_SET_METHOD(target, "add", Add);
}
NODE_MODULE(hellonode, init)
3)在工程属性的配置属性-常规中将输出目录改为.\;
4)在工程属性的配置属性-常规中将目标文件扩展名改为.node;
5)在工程属性的配置属性-C/C+±常规-附加包含目录添加头文件目录为:D:\nodejs\include
6)在工程属性的配置属性-链接器-常规-附加库目录添加目录:D:\nodejs\lib
7)在工程属性的配置属性-链接器-输入-附加依赖项添加lib库:node.lib
8)编译生成后在D:\hellonode中生成一个文件hellonode.node;
9)在D:\hellonode目录创建js测试代码test.js:
var assert = require('assert');
var addons = require('./hellonode');
assert.equal('Hello world!', addons.hello());
console.log('C/C++ addons.hello() =', addons.hello());
console.log('C/C++ addons.add(200, 300) =', addons.add(200, 300));
9)在命令行执行node .\test.js(若未配置node.exe的路径变量,则执行:D:\nodejs\node .\test.js),显示:
C/C++ addons.hello() = Hello world!
C/C++ addons.add(200, 300) = 500
全部代码附件:hellonode.zip,没有找到上传的地方。