POST
(1)body-parser
安装body-parser
npm install -g body-parser
使用
//引入
const bodyParser=require(``"body-parser"``)
// 解析以 application/json 和 application/x-www-form-urlencoded 提交的数据
var jsonParser = bodyParser.json()
var urlencodedParser = bodyParser.urlencoded({ extended: ``false` `})
//json={"path":"./123/1212/123","fileName":"hj"}
app.post("/",urlencodedParser,``function``(req,res){
let data=req.body
let path=data.path
let fileName=data.fileName
res.send(req.body)
})
(2)Express4.16+已经加入了bodyParser,不需要再require,可直接作为express的方法使用.
const express = require('express')
var urlencodedParser = express.urlencoded({ extended: false })
//json={"path":"./123/1212/123","fileName":"hj"}
app.post("/",urlencodedParser,``function``(req,res){
let data=req.body
let path=data.path
let fileName=data.fileName
res.send(req.body)
})
GET
app.post(`"/"`,``function``(req,res){
let appName=req.query["appName"]
res.send(appName)
})