Linux 安装 Node.JS(源码编译安装)
环境:
Ubuntu 12.04.2 LTS (GNU/Linux 3.5.0-23-generic i686)
下载 Node.JS 安装包, 请参考网址: http://nodejs.org/download/
这里选择源码包安装方式, 安装过程如下:
登陆到 Linux 终端, 进入 / usr/local/src 目录, 如下:
root@Ubuntu:~# cd /usr/local/src/
下载 Node.JS 安装包:
#wget http://nodejs.org/dist/v0.10.17/node-v0.10.17.tar.gz
2, 解压文件并安装
# tar xvf node-v0.10.17.tar.gz # cd node-v0.10.17 # ./configure # make # make install # cp /usr/local/bin/node /usr/sbin/ node 查看当前安装的 Node 的版本 # node -v v0.10.17 www.yiibai.com
到此整个安装已经完成, 如果在安装过程有错误问题, 请参考以下解决: 可能出现的问题:
The program 'make' is currently not installed. You can install it by typing: apt-get install make
按照它的提示, 使用命令
# apt-get install make
g++: Command not found 没有安装过 g++, 现在执行安装:
#apt-get install g++
测试程序 hello.JS:
- console.log("Hello World");
- # node helloworld.JS
另外的一个实例: webServer
这个简单 Node 编写的 Web 服务器, 为每个请求响应返回 "Hello World".
var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }).listen(1337); console.log('Server running at port 1337');
要运行服务器, 将代码编写到文件 example.JS 并执行 node 程序命令行:
- # node example.JS
- Server running at http://127.0.0.1:1337/
有兴趣的朋友可以尝试下面一个简单的 TCP 服务器监听端口 1337 并回应的一个例子:
var.NET = require('net'); var server = net.createServer(function (socket) { socket.write('Echo server\r\n'); socket.pipe(socket); }); server.listen(1337, '127.0.0.1');
来源: http://www.bubuko.com/infodetail-2988243.html