您的位置:首页 > 运维架构 > Linux

Linux安装Node.js(源码编译安装)

2015-06-01 16:54 746 查看
环境:

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/

下载nodejs安装包:

#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 -v 

v0.10.17

到此整个安装已经完成,如果在安装过程有错误问题,请参考以下解决: 可能出现的问题:

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');
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: