由于浏览器的同源策略,我们想要调用其他网站的接口,通常会造成跨域。所以我们可以在后端服务中去请求第三方接口,这样就完美避开了浏览器的同源策略。
const http = require('http')
const https = require('https')
const url = require('url')
http.createServer((req, res) => {
const urlobj=url.parse(req.url,true)
res.writeHead(200, {
"Content-Type": "application/json;charset=utf-8",
"access-control-allow-origin": "*"
})
if(urlobj.pathname==='/api'){
httppost((data)=>{
res.end(data)
})
}
}).listen(3000)
function httppost(cb) {
const options = {
hostname: 'h5.qzone.qq.com',
post: '443',
path: "/proxy/domain/w.qzone.qq.com/cgi-bin/likes/internal_dolike_app?g_tk=43316339",
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
cookie: "pgv_pvid=2309791241; eas_sid=V1n655h1y9a3A0A9I7m6U4d0J5; RK=wn10yoKBaM; ptcz=0f26e8389d307b802dd491c61a96d9464ba5d81a773f64c1fbf6f18fcbc2551f; LW_uid=l1C6Z5u3E2R8u1f2s2d1C4T820; LW_sid=r1Z625L3F2Q8H1Q4C6F0F5T5M4; pac_uid=0_016c0af7fdebe; __Q_w_s__QZN_TodoMsgCnt=1; Loading=Yes; QZ_FE_WEBP_SUPPORT=1; cpu_performance_v8=26; uin=o1095518902; skey=@pEG8ihZIF; p_uin=o1095518902; pgv_info=ssid=s7648404939; pt4_token=1MG3I9rTTSis7cWqajCb3e2xtSw9UFL*mDJMvhlDpk4_; p_skey=Z*zM7mV3cCXO8Ad*AatFlZdMwYz207qj1TXJ9WnwNGA_; rv2=8063CD6352ABCAC33A31032701F64066A2E5218458F3895809; property20=0EFF65826771967FFDFBB49CB6109013382923752D687F5051CD16BD92B52C1C653F55D5F1E130FF"
}
}
var req= https.request(options, (res) => {
let data=''
res.on("data", chunk => {
data+=chunk
})
res.on("end", () => {
cb(data)
console.log('ok')
})
})
req.write("qzreferrer=https%3A%2F%2Fuser.qzone.qq.com%2Fproxy%2Fdomain%2Fic2.qzone.qq.com%2Fcgi-bin%2Ffeeds%2Ffeeds_html_module%3Fg_iframeUser%3D1%26i_uin%3D1120341919%26i_login_uin%3D1095518902%26mode%3D4%26previewV8%3D1%26style%3D2%26version%3D8%26needDelOpr%3Dtrue%26transparence%3Dtrue%26hideExtend%3Dfalse%26showcount%3D5%26MORE_FEEDS_CGI%3Dhttp%253A%252F%252Fic2.s21.qzone.qq.com%252Fcgi-bin%252Ffeeds%252Ffeeds_html_act_all%26refer%3D2%26paramstring%3Dos-winxp%7C100&opuin=1095518902&unikey=http%3A%2F%2Fuser.qzone.qq.com%2F1120341919%2Fmood%2F9f0fc742e0bacb5f09e90d00&curkey=http%3A%2F%2Fuser.qzone.qq.com%2F1120341919%2Fmood%2F9f0fc742e0bacb5f09e90d00&from=1&appid=311&typeid=0&abstime=1607187168&fid=9f0fc742e0bacb5f09e90d00&active=0&fupdate=1")
req.end()
}