小程序调用豆瓣接口403 node的解决方案

豆瓣好像禁了小程序的接口调用,返回403错误。网上查了一圈豆瓣应该是通过headers里的referer判断的。

可是wx.request(OBJECT)又不能更改referer。

image

网上都是用nginx来搞的,无奈我这菜B还不会用。。。

所以就自己试着用node做服务器代理。

上代码:

    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")
    });

写了个中间件,headers的设置直接用了普通浏览器里请求api的设置。

    wx.request({
      url: "http://localhost:5000/v2/movie/top250",
      success(res) {
        console.log(res.data);
      }
    })

搞定!

image
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,323评论 19 139
  • 给提问的开发者的建议:提问之前先查询 文档、通过社区右上角搜索搜索已经存在的问题。 写一个简明扼要的标题,并且...
    极乐叔阅读 14,754评论 0 3
  • 转载:https://help.aliyun.com/knowledge_detail/5974693.html?...
    meng_philip123阅读 5,587评论 1 12
  • 在我们开发中常常会遇到这样的情况,当前是列表页面 ,点击后进详情页面,会把列表页面的model传到详情页面,如图:...
    目染江夏阅读 8,844评论 0 4
  • 朋友圈不过是一个被开发出来的应用工具。 就像是把菜刀,是庖丁解牛,还是砍掉了手指头,关刀什么事?要问,还是要问那个...
    whiteqiao阅读 493评论 0 1