关于一个 node.js 的问题,需求是这样,将聚合平台的 API 返回的数据,通过自己的接口包一层,返回聚合平台的数据。我在网上查了些资料,但是并没有成功,所以请教一下技术大大们,求段代码。 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
pingba
V2EX    问与答

关于一个 node.js 的问题,需求是这样,将聚合平台的 API 返回的数据,通过自己的接口包一层,返回聚合平台的数据。我网上查了些资料,但是并没有成功,所以请教一下技术大大们,求段代码。

  •  
  •   pingba 2020-02-11 15:28:24 +08:00 1553 次点击
    这是一个创建于 2151 天前的主题,其中的信息可能已经有所发展或是发生改变。
    具体需要

    1.访问我的接口
    2.我的接口访问聚合接口,接受返回来的数据。
    3.数据一点都不改的返回


    app.get('/api/abc',function(req,res){
    console.log("API 求");


    http.get('http://v.juhe.cn/toutiao/index"', function (res) {
    //console.log("statusCode: ", res.statusCode);
    //console.log("headers: ", res.headers);
    var json = '';
    res.on('data', function (d) {
    json += d;
    });
    res.on('end',function(){
    //获取到的数据
    json = JSON.parse(json);
    res.send(json);

    });
    }).on('error', function (e) {
    res.send(e);

    });



    })


    这是我查询其他资料获取的代码片段,但是并没有达到预期的效果。本人对 node.js 还是小白,请求大大们修改一下。
    11 条回复    2020-02-11 17:23:41 +08:00
    pingba
        1
    pingba  
    OP
       2020-02-11 16:01:54 +08:00
    没人,雷锋一把吗?我顶!
    hyyou2010
        2
    hyyou2010  
       2020-02-11 16:10:59 +08:00
    首先,res.send(json) 是内层 http.get 的 res,或应该是外层 app.get 的 res ?
    pingba
        3
    pingba  
    OP
       2020-02-11 16:13:49 +08:00
    @hyyou2010 外层的,我只是想把内层的 get 请求的数据返回去。

    就是访问 ***/api/abc 这个的时候,获取 http://v.juhe.cn/toutiao/index 的数据
    pingba
        4
    pingba  
    OP
       2020-02-11 16:19:03 +08:00
    @hyyou2010 如何修改,能否直接贴代码,我太小白了。
    hyyou2010
        5
    hyyou2010  
       2020-02-11 16:24:40 +08:00
    你把外层的 res 全部修改为 outerRes 试试看

    app.get('/api/abc', function (req, outerRes) {
    console.log("API 求");


    http.get('http://v.juhe.cn/toutiao/index"', function (res) {
    //console.log("statusCode: ", res.statusCode);
    //console.log("headers: ", res.headers);
    var json = '';
    res.on('data', function (d) {
    json += d;
    });
    res.on('end', function () {
    //获取到的数据
    json = JSON.parse(json);
    outerRes.send(json);

    });
    }).on('error', function () {
    outerRes.send(e);

    });



    })
    hyyou2010
        6
    hyyou2010  
       2020-02-11 16:27:07 +08:00   1
    另外建议在两个 res.on 里面都加打印,看看真的获取到 toutiao 的数据没有
    pingba
        7
    pingba  
    OP
       2020-02-11 16:33:46 +08:00
    @hyyou2010 代码是网上搜的,node 我太业余了,连个调试环境都没有,直接部署宝塔面板做的接口。这段代码估计就有问题。能帮忙跑一下吗?感谢 /
    hyyou2010
        8
    hyyou2010  
       2020-02-11 17:05:03 +08:00
    首先,改 outerRes 那一步应该是没问题。

    其次,你应该用浏览器先访问一下 juhe.cn 那个网址

    访问结果返回:
    resultcode "101"
    reason "错误的请求 KEY"
    result null
    error_code 10001

    说明你请求时需要一个 key,那么代码中还需要增加这个 key
    hyyou2010
        9
    hyyou2010  
       2020-02-11 17:07:35 +08:00
    toutiao/index -------- 就是说这个网址需要一个访问 key
    pingba
        10
    pingba  
    OP
       2020-02-11 17:11:55 +08:00
    @hyyou2010 可能是宝塔不兼容把,不过我用另一段代码成功解决了问题。

    const http =require('http');

    http.get('http://v.juhe.cn/toutiao/index', (res) => {

    const { statusCode } = res;

    const cOntentType= res.headers['content-type'];

    let error;
    if (statusCode !== 200) {
    error = new Error('请求失败\n' +
    `状态码: ${statusCode}`);
    } else if (!/^application\/json/.test(contentType)) {

    error = new Error('无效的 content-type.\n' +//再次报错
    `期望的是 application/json 但接收到的是 ${contentType}`);
    }
    if (error) {
    outerRes.send(error.message);

    res.resume();
    return;
    }

    //请求成功
    res.setEncoding('utf8');
    let rawData = '';
    res.on('data', (chunk) => { rawData += chunk; });
    res.on('end', () => {

    try {


    outerRes.send(rawData);

    } catch (e) {

    outerRes.send(e.message);

    }
    });
    }).on('error', (e) => {

    outerRes.send(e.message);

    });


    换成这个就可以中转数据了。
    hyyou2010
        11
    hyyou2010  
       2020-02-11 17:23:41 +08:00
    嗯嗯,解决了就好
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     1415 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 23ms UTC 16:26 PVG 00:26 LAX 08:26 JFK 11:26
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86