Quantcast
Channel: panjj 最新博客
Viewing all articles
Browse latest Browse all 59

Ubuntu下Nodejs开发环境快速搭建

$
0
0

Nodejs很火,在Ubuntu下搭建它的开发环境尝尝鲜,有一个捷径,它能让系统自动帮你安装所需要的东西,我们生成一段shell脚本,让它来完成以下工作: 安装git下最新的node,node包管理器,Forever和Cloud9IDE工具(可选),mongodb 10gen;脚本的正常运行需要比较新板的Ubuntu,而且需要联网,因为它会连接网络去下载所有的依赖包顺序安装。

#!/bin/sh
# Update System
echo 'System Update'
apt-get update
echo 'Update completed'
apt-get install libssl-dev git-core pkg-config build-essential curl
# Clone Node.js
echo 'Clone Node.js'
cd /usr/src
git clone https://github.com/joyent/node
echo 'Node.js clone completed'
# Install Node.js
echo 'Install Node.js'
cd node
./configure && make && make install
echo 'Node.js install completed'
# Install Node Package Manager
echo 'Install Node Package Manager'
curl http://npmjs.org/install.sh | sh
echo 'NPM install completed'
# Install Forever
echo 'Install Forever'
npm install forever
echo 'Forever install completed'
# Install Cloud9IDE
echo 'Install Cloud9IDE'
git clone git://github.com/ajaxorg/cloud9.git
echo 'Cloud9IDE install completed'
# Install MongoDB
echo 'Install MongoDB'
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10
echo "deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen" >> /etc/apt/sources.list
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10
sudo apt-get update
sudo apt-get install mongodb-10gen
echo 'MongoDB install completed.'
安装方法:

$ cd ~/
$ nano -w node.sh
把以上代码粘贴到node.sh文件里,ctrl+o 保存,ctrl+x 退出nano。如果你没有安装nano,请google一下安装吧。

然后执行脚本:

$ chmod a+x node.sh && sudo ./node.sh

如果网速足够快,一会功夫即可完成安装。等安装完毕,我们来个测试:

mkdir node_project
cd node_project
nano -w server.js

粘贴以下著名的代码:

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(1337, "127.0.0.1");
console.log('Server running at http://127.0.0.1:1337/');
ctrl+o 保存,ctrl+x 退出nano,尝试跑它 :

node server.js
在浏览器中打开 :http://127.0.0.1:1337,看到久违的Hello World了吧?



Viewing all articles
Browse latest Browse all 59

Trending Articles