天天看点

使用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
[email protected] node_modules\debug

[email protected] node_modules\static-favicon

[email protected] node_modules\morgan
└── [email protected]

[email protected] node_modules\cookie-parser
├── [email protected]
└── [email protected]

[email protected] node_modules\body-parser
├── [email protected]
├── [email protected] ([email protected], [email protected])
└── [email protected] ([email protected])

[email protected] node_modules\express
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected] ([email protected], [email protected])
├── [email protected] ([email protected])
└── [email protected] ([email protected], [email protected])

[email protected] node_modules\jade
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected] ([email protected], [email protected], [email protected])
├── [email protected] ([email protected])
├── [email protected] ([email protected])
└── [email protected] ([email protected])
           

光标闪着闪着然后打印出这么一堆的东西,可以知道那个命令安装了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文件夹看看项目的目录结构:

使用expres开发node.js应用

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

继续阅读