使用express创建一个web服务

express是一个快速搭建web服务器的node框架,通过express或者express的cli工具express-generator就能快速的搭建一个http-server…

express

创建一个http-server

1.安装express框架

npm install express --save

2.创建server

1
2
3
4
5
6
const express = require('express')
const app = express()

app.on(3000,()=>{
console.log('server is running on 3000')
})

3.什么是路由

client==》url==》网络传输==》DNS域名解析==》server

根据路由响应客户端的请求
请求包括

4.处理路由

  • 通过请求方式匹配处理 get/post等
1
2
3
4
5
6
7
8
9
10
11
app.get('/list',(req,res)=>{
res.json({
msg:'method is,'+req.method
})
})

app.post('/list',(req,res)=>{
res.json({
msg:'method is,'+req.method
})
})
  • 通过path匹配处理
1
2
3
4
5
6
7
8
9
10
app.get('/api/blog/list',(req,res)=>{
res.json({
msg:'path is,'+req.path
})
})
app.get('/api/user/list',(req,res)=>{
res.json({
msg:'path is,'+req.path
})
})

5.express 常用api

  • all

    (匹配所有请求方式的xxxx)

1
2
3
4
5
app.all('/xxx',(req,res)=>{
res.json({
msg:'match all the methods'
})
})

(匹配所有的uri)

1
2
3
4
5
app.all('*',(req,res)=>{
res.json({
msg:'match all the methods'
})
})
  • use

    建议中间件使用
    使用和all优点类似

1
2
3
4
5
6
7
8
9
10
11
app.use('/xxx',(req,res)=>{
res.json({
msg:'match all the methods'
})
})

app.use((req,res)=>{
res.json({
msg:'match all the methods'
})
})

6.express.Router()路由拆分

在app中使用use匹配根路由

1
2
3
// 注册路由
app.use('/api/blog',blogRouter)
app.use('/api/user',userRouter)

在业务路由中再匹配具体的路由

  • 在blogRouter中定义路由
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    //blogRouter
    const express = require('express')
    const router = express.Router()
    // router
    // .[method]
    // all
    // use

    router.get('/list',(req,res,next)=>{
    res.json({
    msg:'获取列表成功'
    })
    return
    })

    router.post('/new',(req,res,next)=>{
    res.json({
    msg:'新增成功'
    })
    return
    })

    router.post('/del',(req,res,next)=>{
    res.json({
    msg:'删除成功'
    })
    return
    })

    module.exports = router
  • 在app.js中注册拆分的路由
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    // app.js
    const express = require('express')
    const app = express()
    const blogRouter = require('./router/blog.js')
    const userRouter = require('./router/user.js')
    const commonRouter = require('./router/common') // 404
    // 注册路由
    app.use('/api/blog',blogRouter)
    app.use('/api/user',userRouter)
    app.use(commonRouter)

中间件

  • 中间件是一个函数
  • (error,req,res,next)=>{}
  • 分类

    1.app级别的中间件 - 在app注册的时候声明使用 app.use(middleWare)

2.router级别的中间件 - 在路由拆分之后使用 router.use(middleWare)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
const isValid = (req,res,next)=>{
if(req.query&&req.query.id){
next() // 交出控制权
}else{
res.json({
msg:'id不能为空'
})
}
}
// 1. 使用use的方式使用路由中间件======================================
router.use(isValid)
router.get('/list', (req, res) => {
res.json({
msg: '获取列表成功'
})
return
})

// 2.在路由内部使用 =====================================
router.get('/list',isValid, (req, res) => {
res.json({
msg: '获取列表成功'
})
return
})
初到贵宝地,有钱的给个钱场,没钱的挤一挤给个钱场