之前的文章介绍过Express的Demo。下面我们较详细的再介绍一次
Express的介绍
1. Express的安装
npm install express
2. 基本感知
原生的服务器需要按照请求路径,利用js去读取指定路径下文件,然后按照指定类型响应给浏览器进行解析。而现在利用Express开发框架之后,直接调用相关API就可以公开资源,就可以直接根据路径进行请求资源了。
3. 基本路由
路由器:请求方法、请求路径、请求处理函数
4. 模板引擎
5. 处理post请求
(1)安装
npm install body-parser
(2)配置
let bodyparser=require('body-parser')
(3)使用
app.post(‘url’,function(req,res){
var params=req.body //json结构的数据
})
6. 路由分离
(1)router.js
(2)入口文件app.js
【注意】配置art-template与body-parser时,一定要在app.use(router)挂载路由之前。
7. Express中间件
(1)模糊匹配
(2)精准匹配
8. 异常处理
【注意】配置全局错误处理以及404处理的中间件时,一定要在路由的后面。中间件的函数一般都是3个参数:req,res,next,全局错误处理中间件配置4个参数,第一个参数是错误对象,剩下的3个同上。
【提示】请求处理出现错误,直接return next(err)。调用下一次四个参数的应用程序级别的中间件。如果next方法没有参数,就会去匹配下一个符合请求的路由。
扩展:中间件与全局错误处理
var express = require('express')
var app = express()
// 中间件:处理请求的,本质就是个函数
// 在 Express 中,对中间件有几种分类
/* 当请求进来,会从第一个中间件开始进行匹配
如果匹配,则进来
如果请求进入中间件之后,没有调用 next 则代码会停在当前中间件
如果调用了 next 则继续向后找到第一个匹配的中间件
如果不匹配node ,则继续判断匹配下一个中间件
*/
/*
不关心请求路径和请求方法的中间件,也就是说任何请求都会进入这个中间件
中间件本身是一个方法,该方法接收三个参数:
Request 请求对象
Response 响应对象
next 下一个中间件
当一个请求进入一个中间件之后,如果不调用 next 则会停留在当前中间件
所以 next 是一个方法,用来调用下一个中间件的
调用 next 方法也是要匹配的
*/
/*app.use(function (req, res, next) {
console.log('1')
next()
})
app.use(function (req, res, next) {
console.log('2')
next()
})
app.use(function (req, res, next) {
console.log('3')
res.send('333 end.')
})
app.use(function (req, res, next) {
console.log(4)
next()
})*/
/* 以 /xxx 开头的路径中间件
app.use('/a', function (req, res, next) {
console.log('a')
next()
})
app.use(function (req, res, next) {
console.log('2')
next()
})
app.use('/a', function (req, res, next) {
console.log('a 2')
})*/
/* 除了以上中间件之外,还有一种最常用的
严格匹配请求方法和请求路径的中间件
app.get
app.post
app.use(function (req, res, next) {
console.log(1)
next()
})
app.get('/abc', function (req, res, next) {
console.log('abc')
next()
})
app.get('/', function (req, res, next) {
console.log('/')
next()
})
app.use(function (req, res, next) {
console.log('haha')
next()
})
app.get('/abc', function (req, res, next) {
console.log('abc 2')
})
app.use(function (req, res, next) {
console.log(2)
next()
})
app.get('/a', function (req, res, next) {
console.log('/a')
})
*/
// 如果没有能匹配的中间件,则 Express 会默认输出:Cannot GET 路径
app.listen(3000, function () {
console.log('app is running at port 3000.')
})
var express = require('express')
var fs = require('fs')
var app = express()
/* app.get('/abc', function (req, res, next) {
console.log('abc')
req.foo = 'bar'
req.body = {}
next()
})
app.get('/abc', function (req, res, next) {
console.log(req.body)
console.log('abc 2')
})
*/
app.get('/', function (req, res, next) {
fs.readFile('.d/sa./d.sa/.dsa', function (err, data) {
if (err) {
/* return res.status(500).send('Server Error')
当调用 next 的时候,如果传递了参数,则直接往后找到带有 四个参数的应用程序级别中间件
当发生错误的时候,我们可以调用 next 传递错误对象
然后就会被全局错误处理中间件匹配到并处理之
*/
console.log(err)
next(err)
}
})
})
app.get('/', function (req, res, next) {
console.log('/ 2')
})
app.get('/a', function (req, res, next) {
fs.readFile('./abc', function (err, data) {
if (err) {
// return res.status(500).send('Server Error')
next(err)
}
})
})
app.use(function (req, res, next) {
res.send('404')
})
// 配置错误处理中间件
app.use(function (err, req, res, next) {
res.status(500).send(err.message)
})
app.listen(3000, function () {
console.log('app is running at port 3000.')
})