前言
我是歌谣 微信公众号关注前端小歌谣一起学习前端知识 今天继续给大家讲解node中post请求的讲解
案例
var http=require("http")
var url=require("url")
var https=require("https")
http.createServer((req,res)=>{
var urlobj=url.parse(req.url)
res.writeHead(200,{
"content-Type":"application/json;charset=utf-8",
"access-control-allow-origin":"*"
})
switch(urlobj.pathname){
case "/api/aaa":
httppost((data)=>{
res.end(data)
})
break
default:
res.end("404")
}
}).listen(3000)
function httpget(response){
var data=""
https.get(`https://i.maoyan.com/api/mmdb/movie/v3/list/hot.json`,(res)=>{
res.on("data",(chunk)=>{
data+=chunk
})
res.on("end",()=>{
console.log(data)
response.end(data)
})
})
}
function httppost(cb){
var options={
hostname:"m.xiaomiyoupin.com",
port:"443",
path:"/mtop/market/search/placeHolder",
method:"POST",
headers:{
"Content-Type":"application/json"
}
}
var data=""
var req=https.request(options,(res)=>{
res.on("data",chunk=>{
data+=chunk
})
res.on("end",()=>{
cb(data)
})
})
req.write(JSON.stringify([{},{"baseParams":{"ypClient":1}}]))
req.end()
}
运行结果