创建nodejs插件hello.node

1、node介绍

直接看百度百科:http://baike.baidu.com/view/3974030.htm?fr=aladdin

2、安装node

0).预准备

安装gcc:yum install gcc-c++

安装python:2.5-3.0之间,使用 #python -V 查看是否已经最新

1).下载nodejs到本地并解压缩

[root@localhost node]# wget http://nodejs.org/dist/v0.10.24/node-v0.10.24.tar.gz
[root@localhost node]# tar zxvf node-v0.10.24.tar.gz

2).进入到该目录编译和安装

[root@localhost node-v0.10.24]# cd node-v0.10.24
[root@localhost  node-v0.10.24]# ./configure --prefix=/usr/local/node/0.10.24
这里安装在了/usr/local/node/0.10.24目录下
[root@localhost node-v0.10.24]# make
[root@localhost node-v0.10.24]# make install

3).配置NODE_HOME

[root@localhost node-v0.10.24]# vi /etc/profile
在export PATH USER 。。。一行的上面添加如下内容,并将NODE_HOME/bin设置到系统path中
#set for nodejs
export NODE_HOME=/usr/local/node/0.10.24
export PATH=$NODE_HOME/bin:$PATH
保存退出后执行如下命令,使刚才的配置生效
[root@localhost node-v0.10.24]# source /etc/profile

4).执行node -h命令验证设置成功

3、Demo

1).写个nodejs服务器

app.js

var http=require('http');
http.createServer(function(req,res){
    res.writeHead(200, {'Content-Type':'text/plain'});
    res.end('Hello World!\n');
}).listen(8080, '127.0.0.1');

console.log('Server running at http://127.0.0.1:8080');

[root@localhost node-v0.10.24]# node app
Server running at http://127.0.0.1:8080

然后在浏览器中访问:http://127.0.0.1:8080 , 显示Hello World!


退出服务器:ctrl+d

强杀:kill s -9 pid

2).写个nodejs插件(非原生模块hello.node)

1).安装工具

$npm install node-gyp -g

2).具体代码使用该处的:https://github.com/rvagg/node-addon-examples/tree/master/1_hello_world/node_0.10

3).在package.json文件所在的目录执行 npm install 命令即可安装所需要的模块。

4).进行编译

$node-gyp configure

$node-gyp build

5).编写test.js

var addon = require("./build/Release/hello");
console.log(addon.hello());

6).测试test.js

[root@localhost hello]# node test
world



猜你喜欢

转载自blog.csdn.net/gwzz1228/article/details/39962687