第三方API使用本地bash代理将数据从本地计算机发送到其自身(这里是一个简化版本):wget -q -T 25 --post-data "aa=bb&cc=dd" --no-check-certificate "$url"
有问题的机器使用ISP提供的互联网,该ISP阻止了第三方地址,所以我尝试创建一个代理服务器,它将接收来自代理的数据并将其转发到第三方服务器。**问题是转发的req.body作为空对象到达,我不明白为什么。**这里是一个简化的节点。代理API和第三方模拟API失败的js代码:
const http = require('http')
const compression = require('compression')
const express = require('express')
const httpServerPort = process.env.SERVER_PORT /// SET YOUR PORT
const app = express()
app.use(express.urlencoded({ extended: true, limit: '50mb' }))
app.use(compression())
const httpServer = http.createServer(app)
httpServer.listen(httpServerPort, () => {
console.log(`HTTP server started on port ${httpServerPort}`)
})
app.post('/api/v1/agent_proxy', async (req, res) => {
try {
const postData = querystring.stringify(req.body)
// console.log(postData)
const options = {
hostname: 'localhost',
port: httpServerPort,
path: '/api/v1/simu_api',
method: 'POST',
Headers: {
"Content-Type": "application/x-www-form-urlencoded",
"Content-Length": Buffer.byteLength(postData)
}
}
const innerReq = http.request(options, (innerRes) => {
innerRes.on('data', (d) => {
console.log(`statusCode: ${res.statusCode}`)
res.sendStatus(200)
});
});
innerReq.on('error', (error) => {
console.error(error)
res.sendStatus(500)
});
innerReq.write(postData)
innerReq.end()
} catch (err) {
console.error(err)
res.sendStatus(501)
}
})
app.post('/api/v1/simu_api', async (req, res) => {
try {
////////// HERE I CAN SEE THE PROBLEM OF req.body == {} WHILE IT SHOULD BE EQUAL TO {aa: 'bb', cc: 'dd'} /////////
console.log(req.body)
} catch (err) {
console.error(err)
res.sendStatus(501)
}
res.sendStatus(200)
})
1条答案
按热度按时间zwghvu4y1#
在选项对象中,我写了“头”而不是“头”。