express处理表单的post请求
需要我们注意的是express内置了获取get请求体的API(req.query),但是并没有内置获取post请求体的API,所以需要我们手动的去配置中间件。
配置body-parser中间件
1. 安装
npm install body-parser
复制代码
2. 导入并配置
const bodyParser = require('body-parser')
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
复制代码
3. 此时即可成功获取req.body(客户端post的内容)
app.post('/post',(req,res) => {
console.log(req.body);
})
复制代码
[Object: null prototype] { name: 'faithpassi', message: 'dsddfgfgdf' }
复制代码
将读取到的字符串转为JSON
fs.readFile('./db.json','utf8', (err, data) => {
if (!err) {
res.render('index.html', {
students: JSON.parse(data).students
});
}
})
复制代码
将所有的路由结构提取到一个文件中,并进行暴露
方式一:自己封装函数
const fs = require('fs');
module.exports = function (app) {
app.get('/students', (req, res) => {
fs.readFile('./db.json', 'utf8', (err, data) => {
if (!err) {
res.render('index.html', {
students: JSON.parse(data).students
});
}
})
})
}
复制代码
方式二:使用Express自带的路由容器
1.在路由文件中创建路由容器,并进行暴露
const express = require('express');
// 创建一个路由容器
const router = express.Router();
router.get('/students', (req, res) => {
fs.readFile('./db.json', 'utf8', (err, data) => {
if (!err) {
res.render('index.html', {
students: JSON.parse(data).students
});
}
})
})
module.exports = router;
复制代码
2. 将路由容器挂载到app上
// 把路由器挂载到 app上
app.use(router)