Step 1 install nodejs
1.1 install nvm
使用 nvm(Node Version Manage) 来安裝 node.js,sudo apt-get install git-core g++ curl
git clone git://github.com/creationix/nvm.git ~/.nvm
echo ". ~/.nvm/nvm.sh" >> ~/.bashrc重新开启 terminal 或是在当前terminal 输入:source ~/.bashrc或. ~/.bashrc 重新加载bashrc的设定到目前bash环境中。
1.2 install nodejs
nvm install v0.10.26
nvm alias default v0.10.26
node -v PS:node.js 在 0.6.3 之后开始內建 npm (Node Package Management),
可用 npm -v 作确认。
Step 2 install express
Express是目前最流行的基于Node.js的Web开发框架, 使用它可以快速地搭建网站原型,它是一个node.js模块,采用npm全局模块进行安装。
安装命令如下:
npm install -g express安装完成后,在工作目录新建一个新项目,假定叫 helloword。
express helloword此时,会在工作目录下生成一个helloword 子目录,如下:
blwang@blwang-desktop:~/workspace/nodejs_workspace$ express helloword
create : helloword
create : helloword/package.json
create : helloword/app.js
create : helloword/public
create : helloword/public/javascripts
create : helloword/public/images
create : helloword/public/stylesheets
create : helloword/public/stylesheets/style.css
create : helloword/routes
create : helloword/routes/index.js
create : helloword/routes/user.js
create : helloword/views
create : helloword/views/layout.jade
create : helloword/views/index.jade
install dependencies:
$ cd helloword && npm install
run the app:
$ node app根据提示,安装dependencies, 然后运行node app,就可以在本地的3000端口看到生成的默认网站框架。
访问地址:http://127.0.0.1:3000
Step 3 Install MongoDB安装参照http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list
sudo apt-get update
sudo apt-get install mongodb-10gen
mongodb 数据库操作链接如下:http://docs.mongodb.org/manual/core/crud-introduction/
Step4 Install Mongoose
多种中间件可以用于连接node.js与MongoDB,目前比较常用的Mongoose。
首先,在之前利用express新建的helloworld项目目录安装Mongoose,命令如下:
npm install mongoose --save然后,就可以在node.js脚本中连接MongoDB数据库了。
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/<数据库名>');注意,运行上面这个脚本时,必须确保MongoDB处于运行中。
Node.js与Express快速搭建Web应用教程
本文详细介绍了如何使用Node.js、Express、MongoDB和Mongoose等技术栈搭建Web应用,包括安装Node.js与Express,创建Express项目,安装MongoDB数据库,使用Mongoose连接数据库,以及运行应用的基本步骤。
694

被折叠的 条评论
为什么被折叠?



