nodejs + express + ejs + mongodb 一个非常简单的前后端开发的实例

                    THE DEAD-SIMPLE STEP-BY-STEP GUIDE FOR FRONT-END DEVELOPERS TO GETTING UP AND RUNNING WITH NODE.JS, EXPRESS,JADE AND MONGODB

               翻译自: http://cwbuecheler.com/web/tutorials/2013/node-express-mongo/

一 介绍

     There are approximately one hundred million tutorials on the web for getting a "Hello, World!" app running with Node.js. 网上可以获得上亿个用nodejs运行的"Hello world"应用的文章. 如果你的目标仅仅停留在hello这个层次放弃你的web的职业生涯的话,这是非常好的.

  That doesn't really describe most of us, so we go looking for more tutorials. 当然那并不代表我们的大多数人, 所以我们会寻求更多的教程. 

   In my experience, the "next level" tutorials out there seem about 30 levels further along. 据我的经验, "下一级别"的教程似乎还有30级要走. 我们从"Hello World"的应用到创建整个具有评论功能的博客.

  1  Part1 : 15 MINUTES OF INSTALLING 15分钟安装

   我运行在WIN8系统下, 因此与那些MAC 或UBUNTU或*NIX系统有些许的不同, 但是基本原理是一样的.

 A: 第1步安装 node.js 

    非常的简单,访问 Node.js website  的网站, 单击大的绿色的安装按钮.  它装根据你的OS安装相应的应用. 运行installer, 安装完Node.js后, NPM(Node Package Manager), 它非常重要,作用是快速简单的添加各种stuff到Node机器中.

  •      打开命令窗口
  •      cd 创建一个目录, 用来存放你希望的测试应用的路径.(本文的测试应用的目录是: C:\node) 
    B: 第2步: 安装express

    现在我们的node开始运行了, 实现我们在创建一个可用的web网站的时候, 我们还需求其他的工具. 我们将要安装Express, Express是一个框架, 将单纯的nodejs转变成更像一个我们曾经用过的web服务器.

C:\node>npm install -g express
      This installs some core Express functionality right into our Node installation, making it available globally so we can use it anywhere we want. That's handy. You'll see a bunch of text in your command prompt, mostly a lot of http 304's and GETs. That's fine. Express is now installed and available.

    这样就安装了一些Express的核心功能,  -g 是使它成为了全局可胜,因此我们可以在任何地方应用, 那样很方便. 你将会在你的命令窗口中见到一连串的文本, 大部分是http请求或get请求, 非常好,现在Express已经安装成功并可用了.

   C: 第3步: 创建一个Express工程 

C:\node>express --sessions nodetest1

   点击Enter你将会看到如下的内容: 

C:\node>express --sessions nodetest1
create : nodetest1
create : nodetest1/package.json
create : nodetest1/app.js
create : nodetest1/routes
create : nodetest1/routes/index.js
create : nodetest1/routes/user.js
create : nodetest1/views
create : nodetest1/views/layout.jade
create : nodetest1/views/index.jade
create : nodetest1/public/images
create : nodetest1/public/javascripts
create : nodetest1/public
create : nodetest1/public/stylesheets
create : nodetest1/public/stylesheets/style.css

install dependencies:
$ cd nodetest1 && npm install

run the app:
$ node app
D: 第4步: 编辑依赖

  OK, now we have some basic structure in there, but we're not quite done. You'll note that the express installation routine created a file called package.json in your nodetest1 directory. Open this up in a text editor and it'll look like this:   

{
    "name": "application-name",
    "version": "0.0.1",
    "private": true,
    "scripts": {
        "start": "node app.js"
    },
    "dependencies": {
        "express": "3.3.6",
        "jade": "*"
    }
}

这是基本的JSON文件描述我们的应用及它的依赖关系. 我们需要添加一些内容. 特别是我们需要引入MongoDB和Monk, 让我们加入依赖对象如下:

"dependencies": {
    "express": "3.3.6",
    "ejs": "*",
    "mongodb": "1.3.19",
    "monk": "0.7.1"
}
 
 

      E: 安装依赖    

    Now we're ready to go. Note that those version numbers might be out of date by the time you read this article. You can check them from your command prompt with "npm info [dependency] version" (eg: npm info mongodb version). Return to your command prompt, cd to your nodetest1 directory, and type this:

   现在我们继续. 注意这些版本号在你读到这篇文章的时候可能已经过时了. 你现在从你的命令窗口中(npm into[dependency] version)检测他们.  返回你的命令窗口, 创建nodetest1目录, 输入如下信息: 

C:\node\nodetest1>npm install
    It's going to print out a ton of stuff. That's because it's reading the JSON file we just edited and installing all the stuff listed in the dependencies object (yes, including Express – we installed the top level stuff using the –g flag, but we still have to install some necessary code for this particular project). Once NPM has run its course, you should have a node_modules directory which contains all of our dependencies for this tutorial.

   一旦你运行了npm , 将会生成一个node_modules目录用来包含文章中所有的我们的依赖.

   到现在我们有了一个完全可以运行的应用, 让我们测试一下. 目录转到你的nodetest1目录下, 输入 

C:\node\nodetest1>node app.js
 Express server listening on port 3000

    打开浏览器输入 http://localhost:3000 你将会看到 Express 的欢迎页面. 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值