豆瓣好像禁了小程序的接口調(diào)用宾毒,返回403錯(cuò)誤驼修。網(wǎng)上查了一圈豆瓣應(yīng)該是通過headers里的referer判斷的。
可是wx.request(OBJECT)又不能更改referer诈铛。
網(wǎng)上都是用nginx來搞的乙各,無奈我這菜B還不會(huì)用。癌瘾。觅丰。
所以就自己試著用node做服務(wù)器代理。
上代碼:
var express = require("express");
var app = express();
var proxy = require("http-proxy-middleware");
var cors = require("cors");
var apiproxy = [proxy("/v2/movie/top250", {
target: "https://api.douban.com",
changeOrigin: true
}), proxy("/*", {
target: "http://localhost:3000",
changeOrigin: true
})];
app.use(cors());
app.use((req, res, next) => {
req.headers = {
"accept": "*/*",
"accept-encoding": "gzip, deflate, sdch, br",
"accept-language": "zh-CN,zh;q=0.8",
"cache-control": "no-cache",
"connection": "keep-alive",
"host": "localhost:5000",
"origin": "http://localhost:8080",
"pragma": "no-cache",
"referer": "http://localhost:8080/",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36"
}
next();
});
app.use((req, res, next) => {
console.log(req.headers.referer);
next();
})
app.use(apiproxy);
app.listen(5000, () => {
console.log("port on 5000")
});
寫了個(gè)中間件妨退,headers的設(shè)置直接用了普通瀏覽器里請(qǐng)求api的設(shè)置妇萄。
wx.request({
url: "http://localhost:5000/v2/movie/top250",
success(res) {
console.log(res.data);
}
})
搞定!