express中的req.body后面附带属性是什么意思
发布于 3 年前 作者 sunboy25 2129 次浏览 来自 问答

var express = require(‘express’); var router = express.Router(); var Device = require("…/models/device") var messageService = require("…/services/message_service")

router.post("/", function (req, res) { switch (req.body.action){ case “client_connected”: Device.addConnection(req.body) break case “client_disconnected”: Device.removeConnection(req.body) break; case “message_publish”: messageService.dispatchMessage({ topic: req.body.topic, payload: new Buffer(req.body.payload, ‘base64’), ts: req.body.ts }) } res.status(200).send(“ok”) })

module.exports = router 我想问一下这段代码的req.body后面带的topic,payload,action这些属性在哪里能查到它的具体用途,在官网http://expressjs.com/en/4x/api.html#req.body没找到。

5 回复

这些是业务逻辑参数,由请求端传过来的数据, 这个应该是推送相关服务吧

@wucpeng 那这些参数能在那里找到对它的说明?

初学者,了解是get和post在express里咋处理就清楚了。req.body是处理post类请求的请求体上的内容,常见是表单提交post,狼书卷二node web应用开发里有详细说明

req.body 指的是前端/别的客户端发送的 http 请求带的参数

比如前端发送了一个 POST 请求,参数是 {a: 1, b: 2},那么 Express 对应的接口接收到的 req.body 就是 {a: 1, b: 2}

@hyj1991 谢谢大神,总算搞明白了!

回到顶部