Ubuntu 12.0.4 下安装 Nodejs

Ubuntu 12.0.4 下安装 Nodejs

准备工作

1
sudo apt-get install g++ curl libssl-dev apache2-utils

安装方式(一):git clone

1
2
sudo apt-get install git-core
git clone git://github.com/ry/node.git

安装方式(二): 源码安装

1
2
3
wget http://nodejs.org/dist/v0.10.13/node-v0.10.13-linux-x64.tar.gz
mv node-v0.10.13-linux-x64.tar.gz node-linux-x64.tar.gz
tar -zxvf node-linux-x64.tar.gz

编译和安装 Node

1
2
3
4
cd your-node-current-folder
./configure
make
sudo make install

查看是否安装成功

1
node -v OR node -version //查看Node.js当前的版本

Node 运行 hello world

在主文件夹创建 hello.js,编辑以下文本:

1
2
3
4
5
6
7
8
9
10
var http = require("http");

http
.createServer(function (req, res) {
res.writeHead(200, { "Content-Type": "text/plain" });
res.end("Hello world!");
})
.listen(3000, "127.0.0.1");

console.log("Server running at http://127.0.0.1:3000/");

在命令行中运行

1
node hello.js

打开浏览器,输入http://127.0.0.1:3000/,可以看到屏幕上显示"Hello world!"