使用expres开发node.js应用

express是node.js中web开发的mvc开发框架,支持jade,ejs等多种模板引擎

我们先看看用express的http模块如何进行web开发。

var http = require('http');
var querystring = require('querystring');
var server = http.createServer(function(req, res) {
var post = '';
req.on('data', function(chunk) {
post += chunk;
});
req.on('end', function() {
post = querystring.parse(post);
res.write(post.title);
res.write(post.text);
res.end();
});
}).listen(3000);
简单的几行代码一个web应用就搭建完毕,保存为server.js,然后再当前文件夹中执行node server.js,接下来我们就可以用浏览器来访问这个web应用了。

如果用这种方式进行web开发,那么请求解析,请求路由,会话管理等都需要自己去实现,那么需要做的事情真是太多了。

express这个mvc开发框架提供了:路由控制,模板解析,动态视图等众多的功能。

首先我们需要安装express,安装方式可以自行百度,安装成功之后:

C:\Users\Administrator>express hello

   create : hello
   create : hello/package.json
   create : hello/app.js
   create : hello/public
   create : hello/public/javascripts
   create : hello/public/images
   create : hello/public/stylesheets
   create : hello/public/stylesheets/style.css
   create : hello/routes
   create : hello/routes/index.js
   create : hello/routes/users.js
   create : hello/views
   create : hello/views/index.jade
   create : hello/views/layout.jade
   create : hello/views/error.jade
   create : hello/bin
   create : hello/bin/www

   install dependencies:
     $ cd hello && npm install

   run the app:
     $ DEBUG=hello ./bin/www


我们可以看到在当前目录下面创建了一大堆的文件,我们先不去看这些文件,看看

 install dependencies:
     $ cd hello && npm install


这段话的意思是该项目依赖与其他的模块,请执行cd hello命令,进入到hello文件夹执行npm install命令,我们照指示的办:

C:\Users\Administrator>cd hello

C:\Users\Administrator\hello>npm install
debug@0.7.4 node_modules\debug

static-favicon@1.0.2 node_modules\static-favicon

morgan@1.0.1 node_modules\morgan
└── bytes@0.3.0

cookie-parser@1.0.1 node_modules\cookie-parser
├── cookie-signature@1.0.3
└── cookie@0.1.0

body-parser@1.0.2 node_modules\body-parser
├── qs@0.6.6
├── raw-body@1.1.7 (string_decoder@0.10.25-1, bytes@1.0.0)
└── type-is@1.1.0 (mime@1.2.11)

express@4.2.0 node_modules\express
├── qs@0.6.6
├── parseurl@1.0.1
├── debug@0.8.1
├── escape-html@1.0.1
├── fresh@0.2.2
├── cookie-signature@1.0.3
├── buffer-crc32@0.2.1
├── methods@1.0.0
├── serve-static@1.1.0
├── utils-merge@1.0.0
├── merge-descriptors@0.0.2
├── range-parser@1.0.0
├── path-to-regexp@0.1.2
├── cookie@0.1.2
├── send@0.3.0 (debug@0.8.0, mime@1.2.11)
├── type-is@1.1.0 (mime@1.2.11)
└── accepts@1.0.1 (mime@1.2.11, negotiator@0.4.7)

jade@1.3.1 node_modules\jade
├── commander@2.1.0
├── character-parser@1.2.0
├── mkdirp@0.3.5
├── transformers@2.1.0 (css@1.0.8, promise@2.0.0, uglify-js@2.2.5)
├── with@3.0.0 (uglify-js@2.4.15)
├── monocle@1.1.51 (readdirp@0.2.5)
└── constantinople@2.0.1 (uglify-js@2.4.15)

光标闪着闪着然后打印出这么一堆的东西,可以知道那个命令安装了jade,express,body-parser,cookie-parser这些模块,那么npm如何知道这个项目依赖于这些模块的呢?

我们进入到hello文件夹下,打开package.json这个文件可以看到

  "dependencies": {
    "express": "~4.2.0",
    "static-favicon": "~1.0.0",
    "morgan": "~1.0.0",
    "cookie-parser": "~1.0.1",
    "body-parser": "~1.0.0",
    "debug": "~0.7.4",
    "jade": "~1.3.0"
  }
我们在看看

   run the app:
     $ DEBUG=hello ./bin/www
意思是该项目的启动文件是bin文件夹下面的www文件,之前老的版本是执行node app.js启动项目。


下面我们进入hello文件夹看看项目的目录结构:


bin下面放置的是项目的启动文件,node_modules放置的是其依赖的包,刚刚执行npm -install安装的包就放置在这个目录下面。

public放置项目的源码,包括图片文件,js代码以及样式文件表。routes从名字上面来看就知道存放着一些路径路由信息,views存放页面模板。


我们看看bin文件夹下面的www问价:

#!/usr/bin/env node
var debug = require('debug')('hello');
var app = require('../app');

app.set('port', process.env.PORT || 3000);

var server = app.listen(app.get('port'), function() {
  debug('Express server listening on port ' + server.address().port);
});
发现这里面设置了web服务器监听的端口,其中引入了app模块。

再看看app.js

app.use('/', routes);
app.use('/users', users);
app.use([path], function)定义了中间件的使用,可选参数默认为“/”,需要注意的是定义中间件的顺序,执行的顺序是和定义的顺序一样从上至下的。

对应的请求链接请求会被对应的中间件响应,我们看看routes/index.js文件

var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res) {
  res.render('index', { title: 'Express' });
});

module.exports = router;
index.js文件相当于一个控制器,用于组织展示的内容

res.render(view, [locals], [callback])

callback函数接受渲染之后的字符串,如果不需要处理那么可以不传该参数,那么渲染后的字符串会被直接输出至请求方。





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值