npm i express@4.17.1
//导入express
const express = require('express')
//创建web服务器
const server = express()
//调用server.lieten(端口号,启动完成后的回调函数),启动服务器
server.listen('80',()=>{
console.log('express server running at http://127.0.0.1')
})

//4.监听客户端的GET和POST请求,并向客户端响应具体的内容,这里这个监听行为相当于http里面的request请求,当时我们做的是直接将那个网址在浏览器运行,现在时使用postman发送这个请求,这边写的代码是监听他的请求。
//打印的在postman工具里面
server.get('/user',(req,res)=>{
//调用express提供的res.send()方法,向客户端提供一个JSON对象
res.send({name:'zx',age:20,gender:'男'})
})
server.post('/user',(req,res)=>{
res.send('请求成功')
})


我们先编写代码,然后重新在终端运行一下之后,使用postman工具进行测试get http://127.0.0.1/?name = sz&age = 20,这种方式直接和我们在下面写键值对是一样的
//这个/是地址,在后面拼接的是参数
server.get('/',(req,res)=>{
//通过req.query可以获取到客户端发送过来的查询参数
//默认情况下,req.query是一个空对象
console.log(req.query)//{}
res.send(req.query)
//我们可以使用req.query.name , req.query.age访问到查询参数
})
//返回值
{
"name":"sz",
"age":"20"
}

在写完代码之后,我们可以使用postman发送请求 get http://127.0.0.1/user/1 send
//注意:这里的:id是一个动态参数
server.get('/user/:id',(req,res)=>{
//req.params是动态匹配到的url参数,默认也是一个空对象
console.log(req.params)
res.send(req.params)
})

注意点
const express = require('express')
const server = express()
//在这里,调用express.static()方法,快速的对外提供静态资源,这里写的就是路径
server.use(express.static('./clock'))
server.listen('8081',()=>{
console.log('express server running at http://127.0.0.1:8081')
})


app.use(express.static('./clock'))
app.use(express.static('./files'))
// 写这个就相当于创建了一个web服务器,我们使用node运行服务器,将资源托管出去,可使用网址进行访问
const express = require('express')
const app = express()
app.use('/abc',express.static('./clock'))
app.use(express.static('./files'))
app.listen('8081',()=>{
console.log('express server running at http://127.0.0.1:8081')
})


安装 npm install -g nodemon
const express = require('express')
const app = express()
app.use('/files',express.static('./clock'))
app.use(express.static('./files'))
app.listen('8081',()=>{
console.log('express server running at http://127.0.0.1:8081')
})


app.method(path,handle)
最简单的用法const express = require('express')
const app = express()
app.get('/',(req,res)=>{
res.send('get request')
})
app.post('/',(req,res)=>{
res.send('post request')
})
app.listen('8081',()=>{
console.log('http://127.0.0.1:8081')
})

const express = require('express')
const app = express()
app.listen('8081',()=>{
console.log('http://127.0.0.1')
})
const express = require('express')
const router = express.Router()
router.get('/',(req,res)=>{
res.send('get user list')
})
router.post('/',(req,res) =>{
res.send('Add new user')
})
module.exports = router

const express = require('express')
const app = express()
//1.导入路由模块
const router = require('./03.router')
//2.注册路由模块
app.use(router)
//app.use()函数的作用,就是来注册全局中间件 app.use(express.static('./files')
app.listen('8082',()=>{
console.log('http://127.0.0.1')
})

//和托管静态资源一样
app.use('/yili',router)

可参考https://bbs.huaweicloud.com/blogs/368470
express的中间件,本质上就是一个function处理函数,格式如下

区别路由处理函数和中间件处理函数:就是看他的处理函数参数有没有包含next参数
中间件函数的形参列表中,必须包含 next 参数。而路由处理函数中只包含 req 和 res。

const express = require('express')
const app = express()
const nw = function(res,req,next){
console.log('这是最简单的中间件函数')
//当前业务处理完毕后,调用next函数,把流转关系,转交给下一个中间件或路由
next()
}
app.listen('8081',()=>{
console.log('http://127,0,0,1')
})

const nw = function(res,req,next){
console.log('这是最简单的中间件函数')
//当前业务处理完毕后,调用next函数,把流转关系,转交给下一个中间件或路由
next()
}
//全局生效的中间件
app.use(nw)
app.get('/',(req,res)=>{
res.send('/ page')
})



app.use(function(res,req,next){
console.log('这是最简单的中间件函数')
//当前业务处理完毕后,调用next函数,把流转关系,转交给下一个中间件或路由
next()
})
app.get('/',(req,res)=>{
res.send('/ page')
})



const express = require('express')
const app = express()
// 定义第一个全局中间件
app.use((req, res, next) => {
console.log('调用了第1个全局中间件')
next()
})
// 定义第二个全局中间件
app.use((req, res, next) => {
console.log('调用了第2个全局中间件')
next()
})
// 定义一个路由
app.get('/user', (req, res) => {
res.send('User page.')
})
app.listen(80, () => {
console.log('http://127.0.0.1')
})



const nw = function(req,res,next){
next()
}
//应用级别的中间件(全局中间件)
app.use((req,res,next)=>{
next()
})
//应用级别的中间件(局部中间件)
app.get('/','nw',(req,res)=>{
res.send('/ page')
})
const router = express.Router()
//路由级别的中间件
router.use((req,res,next)=>{
next()
})
module.exports = router
const app = express()
app.use('/',router)
作用:专门用来捕获整个项目中发生的异常错误,从而防止项目异常崩溃的问题。
格式:错误级别中间件的处理函数中,必须有四个形参,形参顺序从前到后,分别是(err,req,res,next)
app.get('/',(req,res)=>{ //1路由
throw new Error('服务器内部发生了错误!') //2 抛出了一个错误
res.send('Home page')
)
app.use((err,req,res,next)=>{// 错误级别的中间件
console.log('发生了错误'+err.message)
res.send('error'+ err.message)
})
1.express.static快速托管静态资源的内置中间件,例如:HTML文件,图片,css样式等(无兼容性)
2.express.json解析json格式的请求体数据(有兼容性,仅在4.16.0+版本中可用)
3.express.urlencoded解析URL-encoded格式的请求体数据(有兼容性,仅在4.16.0+版本中可用)
//配置解析application/json格式数据的内置中间件
app.use(express.json())
//配置解析application/x-www-form-urlencoded格式数据的内置中间件
app.use(express.urlencoded({extended:false}))
const express = require('express')
const app = express()
//除了错误级别的中间件,其他的中间件,必须在路由之前进行配置
//通过express.json这个中间件,解析表格中json数据格式
app.use(express.json())
app.post('/',(req,res)=>{
//在服务器下,可以使用req.body这个属性,来接受客户端发送过来的请求体数据
//默认情况下,如果不配置解析表单数据的中间件,则req.body默认等于undefined
console.log(req.body)
res.send('ok')
})
app.listen('8081',()=>{
console.log('http://127.0.0.1:8081')
})


const express = require('express')
const app = express()
//除了错误级别的中间件,其他的中间件,必须在路由之前进行配置
//通过express.json这个中间件,解析表格中json数据格式
app.use(express.urlencoded({extended:false}))
// app.use(express.json())
app.post('/',(req,res)=>{
//在服务器下,可以使用req.body这个属性,来接受客户端发送过来的请求体数据
//默认情况下,如果不配置解析表单数据的中间件,则req.body默认等于undefined
console.log(req.body)
res.send('ok')
})
app.listen('8081',()=>{
console.log('http://127.0.0.1:8081')
})


例如:在express@4.16.0之前的版本中,经常使用body-parser这个第三方中间件,来解析请求体数据,步骤如下:
1.运行npm install body-parser 安装中间件
2.使用require导入中间件
3.调用 app.use()注册并使用中间件
const express = require('express')
const app = express()
const parser = require('body-parser')
app.use(parser.urlencoded({extend:false}))
app.post('/',(req,res)=>{
//在服务器下,可以使用req.body这个属性,来接受客户端发送过来的请求体数据
//默认情况下,如果不配置解析表单数据的中间件,则req.body默认等于undefined
console.log(req.body)
res.send('ok')
})
app.listen('8081',()=>{
console.log('http://127.0.0.1:8081')
})


实现步骤:
1.定义中间件
2.监听req的data事件
3.监听req的end事件
4.使用querystring模块解析请求体数据
5.将解析出来的数据对象挂载为req.body
6.将自定义中间件封装为模块
const express = require('express')
const qs = require('querystring')
const app = express()
//解析表单数据的中间件
//1.定义中间件
app.use((req,res,next)=>{
//定义具体的业务逻辑
//2.监听req的data事件
//如果数据量比较大,无法一次性发送完毕,则客户端会把数据切割后,分批送到服务器,所以data事件
//可能会多次触发,每一次触发,获取到的数据只是完整数据的一部分,需要手动对接受到的数据进行拼接
let str = ''
req.on('data' ,(chunk)=>{
//拼接 隐式转换为字符串
str += chunk
})
//当请求体接收完毕之后,会自动触发req的end事件,因此我们可以在req的end事件中,拿到并处理完整的请求体数据
//3.监听req的end事件
req.on('end',()=>{
//str中存放的是完整的请求体数据
console.log(str) //name=xx&gender=%E7%94%B7
//把字符串格式的请求体数据,解析成对象格式
//node.js内置了一个querystring模块,专门用来处理查询字符串,通过这个模块提供的parse()函数,
// 可以轻松把查询字符串,解析成对象的格式
//4.使用querystring模块解析请求体数据
const body = qs.parse(str)
// 5.将解析出来的数据对象挂载为req.body
req.body = body
console.log(body)
next()
})
})
app.post('/',(req,res)=>{
res.send(req.body)
})
app.listen('8081',()=>{
console.log('http://127.0.0.1:8081')
})

const express = require('express')
const app = express()
//导入自己封装的中间件模块
const bodyparser = require('./09.my -body-parser')
//将自己定义的中间件函数,注册为全局可用的中间件
app.use(bodyparser)
app.post('/',(req,res)=>{
res.send(req.body)
})
app.listen('8081',()=>{
console.log('http://127.0.0.1:8081')
})
const qs = require('querystring')
const bodyparser = (req,res,next)=>{
let str = ''
req.on('data' ,(chunk)=>{
//拼接 隐式转换为字符串
str += chunk
})
req.on('end',()=>{
console.log(str)
const body = qs.parse(str)
req.body = body
console.log(body)
next()
})
}
module.exports = bodyparser
1.创建基本的服务器
2.创建api路由模块
3.编写GET接口
4.编写post接口
const express = require('express')
const app = express()
//配置解析表单数据的中间件
app.use(express.urlencoded({extend:false}))
const router = require('./11、apirouter')
app.use('/api',router)
app.listen('8081',()=>{
console.log('http://127.0.0.1:8081')
})
const express = require('express')
const router = express.Router()
router.get('/get',(req,res)=>{
//1.获取到客户端通过查询字符串,发送到服务器的数据
const query = req.query
//2.调用res.send方法,把数据响应给客户端
res.send({
status:0, //状态:0表示成功,1表示失败
msg:'get请求成功', //状态描述
data:query //需要响应给客户端的具体数据
})
})
router.post('/post',(req,res)=>{
//1.获取到客户端通过请求体,发送到服务器的url - encoded数据
const body = req.body
//2.调用res.send方法,把数据响应给客户端
res.send({
status:0, //状态:0表示成功,1表示失败
msg:'get请求成功', //状态描述
data:body //需要响应给客户端的具体数据
})
})
module.exports = router

