const http = require('http')
/** 需要反向代理的域名 */
const domain = 'dogwin1.zhouyouling.com'
http.createServer((req, res) => {
if (req.url == '/favicon.ico') return
((headers) => {
delete headers['host']
delete headers['accept-encoding']
delete headers['content-length']
delete headers['connection']
})(req.headers)
let postData = ''
req.on('data', (chunk) => {
postData += chunk
})
req.on('end', () => {
const request = http.request({
hostname: domain,
port: 80,
path: req.url,
method: 'POST',
headers: req.headers
}, (response) => {
let result = ''
response.on('data', (chunk) => {
result += chunk
})
response.on('end', () => {
res.writeHead(200, {
'Content-Type': response.headers['content-type'],
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Method': '*'
})
res.end(result.toString())
})
})
request.write(postData)
request.end()
})
}).listen(8000)
现在看来,该方法不是很好用,只能反代简单的请求,且开发较为麻烦。
现在推荐使用 http-proxy 库,诸如文件上传、POST 请求、复杂请求头等,都能很简单实现。
这主题颜色,看得眼睛都瞎了
纯色主题
确实啊,很难受
博主你大学时期开发的,鹏创翻译exe,的那个api调用文档打不开了,我想学习一下接入阿里翻译(https://translate.alibaba.com/
)