Get into the habit of writing together! This is the 10th day of my participation in the "Nuggets Daily New Plan · April Update Challenge", click to view the details of the event .
Description of Requirement
Simulate a middleware like express.urlencoded
this to parse the form data POSTed to the server
Implementation steps
- Define middleware
- Listen to the data event of req
- Listen to the end event of req
- Parse request body data using querystring module
- Mount the parsed data object as req.body
- Encapsulate custom middleware as modules
Implementation
-
Define middleware
const express = require('express') const app = express() // 定义解析表单数据的中间件 app.use((req, res, next) => { // 定义中间件具体的业务逻辑 }) app.listen(80, ()=> { console.log('express 服务器运行在 http://127.0.0.1.80') }) 复制代码
-
Listen to the data event of req
In the middleware, you need to listen to the data event of the req object to get the data sent by the client to the server
If the amount of data is relatively large and cannot be sent at one time, the client will cut the data and send it to the server in batches. Therefore, the data event may be triggered many times. Each time the data event is triggered, the acquired data is only a part of the complete data, and the received data needs to be spliced manually.
const express = require('express') const app = express() // 定义解析表单数据的中间件 app.use((req, res, next) => { // 定义中间件具体的业务逻辑 //1、定义一个空字符串,用来存放客户端发送来的请求体数据 let str = '' //2、监听 req 的 data 事件 req.on('data', (chunk)=>{ str += chunk }) }) app.listen(80, ()=> { console.log('express 服务器运行在 http://127.0.0.1.80') }) 复制代码
-
Listen to the end event of req
When the request body data is received, the req and end events will be triggered automatically. Therefore, the complete request body data can be obtained and processed in the end event of req.
const express = require('express') const app = express() // 定义解析表单数据的中间件 app.use((req, res, next) => { // 定义中间件具体的业务逻辑 //1、定义一个空字符串,用来存放客户端发送来的请求体数据 let str = '' //2、监听 req 的 data 事件 req.on('data', (chunk)=>{ str += chunk }) //3、监听 req 的 end 事件 req.on('end', () => { // 在 str 中存放的时完整的请求体数据 console.log(str) }) }) app.post('/user', (req, res) => { res.send('ok') }) app.listen(80, ()=> { console.log('express 服务器运行在 http://127.0.0.1.80') }) 复制代码
-
Parse request body data using querystring module
Node.js has a built-in querystring module, which is specially used to process query strings. Through the
parse()
functions , you can easily parse the query string into an object format.const express = require('express') //导入处理 querystring 的 node.js 内置模块 const qs = require('querystring') const app = express() // 定义解析表单数据的中间件 app.use((req, res, next) => { // 定义中间件具体的业务逻辑 //1、定义一个空字符串,用来存放客户端发送来的请求体数据 let str = '' //2、监听 req 的 data 事件 req.on('data', (chunk)=>{ str += chunk }) //3、监听 req 的 end 事件 req.on('end', () => { // 在 str 中存放的时完整的请求体数据 console.log(str) //4、把字符串格式的请求体数据,解析成对象格式 const body = qs.parse(str) console.log(body) }) }) app.post('/user', (req, res) => { res.send('ok') }) app.listen(80, ()=> { console.log('express 服务器运行在 http://127.0.0.1.80') }) 复制代码
-
Mount the parsed data object as
req.body
A req and res are shared between upstream middleware and downstream middleware and routers. Therefore, the parsed data can be mounted as a custom attribute of req named req.body for downstream use
const express = require('express') //导入处理 querystring 的 node.js 内置模块 const qs = require('querystring') const app = express() // 定义解析表单数据的中间件 app.use((req, res, next) => { // 定义中间件具体的业务逻辑 //1、定义一个空字符串,用来存放客户端发送来的请求体数据 let str = '' //2、监听 req 的 data 事件 req.on('data', (chunk)=>{ str += chunk }) //3、监听 req 的 end 事件 req.on('end', () => { // 在 str 中存放的时完整的请求体数据 console.log(str) //4、把字符串格式的请求体数据,解析成对象格式 const body = qs.parse(str) console.log(body) //5、将解析出来的请求对象,挂载为 req.body req.body = body next() }) }) app.post('/user', (req, res) => { res.send('ok') }) app.listen(80, ()=> { console.log('express 服务器运行在 http://127.0.0.1.80') }) 复制代码
-
Encapsulate custom middleware as modules
In order to optimize the structure of the code, custom middleware functions can be encapsulated into independent modules
-
Create a custom module named custom-body-parser, and extract the previously customized middleware functions and
module.exports
export//导入处理 querystring 的 node.js 内置模块 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) console.log(body) req.body = body next() }) } module.exports = bodyParser 复制代码
-
external call. Import custom modules and register as globally available middleware
const express = require('express') const app = express() // 1、导入自己封装的中间件模块 const customBodyParse = require('./custom-body-parser') // 2、将自定义的中间件函数,注册为全局可用的中间件 app.use(customBodyParse) app.post('/user', (req, res) => { res.send(req.body) }) app.listen(80, ()=> { console.log('express 服务器运行在 http://127.0.0.1.80') }) 复制代码
-