express请求数据的获取(get和post)body-parser
get请求
直接用res.query就可以拿到数据
post请求
需要使用中间件body-parser
第一步:安装body-parser
npm i body-parser
第二步:按照模板进行使用
var express = require('express') var bodyParser = require('body-parser') var app = express() // 中间件bodyparser的配置固定步骤 app.use(bodyParser.urlencoded({ extended: false })) app.use(bodyParser.json()) // req.body就是请求的数据了 app.use(function (req, res) { res.setHeader('Content-Type', 'text/plain') res.write('you posted:\n') res.end(JSON.stringify(req.body, null, 2)) })
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步