简体   繁体   English

NodeJS,Express和带有外部请求的路由

[英]NodeJS, Express and routing with external requests

So I'm currently building a small personal app for learning, I have a scenario where I was to use the API's of other third parties, but several for 1 object. 因此,我目前正在构建一个用于学习的小型个人应用程序,在这种情况下,我将使用其他第三方的API,但对于一个对象,则使用多个API。 For instance, I have 1 dashboard to render and I want to make different calls to different places and product an object of data back that contains the three request responses. 例如,我要渲染1个仪表板,我想对不同的地方进行不同的调用,并生成一个包含三个请求响应的数据对象。

For example at the moment I have something (similar to) the below, which makes a request and returns that data, along with the other data from the find method and current user session. 例如,目前我有以下内容(类似于),它发出一个请求并返回该数据,以及来自find方法和当前用户会话的其他数据。 Works great. 效果很好。

app.get('/profile', isLoggedIn, function(req, res) {
    Object.find().exec(function (err, records) {
        request('firstUrl', function(err, resp, body) {
            res.render('profile.ejs', {
                user : req.user,
                records : records,
                responseData : JSON.parse(body)
            });
        });
    });
});

But now I want to make more than 1 request...so I'm not sure about how to go about this and also best practice? 但是现在我要提出多个请求...所以我不确定如何进行此操作以及最佳实践?

The below feels like it won't work and messy: 以下感觉像是行不通的,凌乱的:

app.get('/profile', isLoggedIn, function(req, res) {
    Object.find().exec(function (err, records) {
        request('firstUrl', function(err, resp, body1) {
            request('secondUrl', function(err, resp, body2) {
                request('thirdUrl', function(err, resp, body3) {
                    res.render('profile.ejs', {
                        user : req.user,
                        records : records,
                        responseData1 : JSON.parse(body1),
                        responseData2 : JSON.parse(body2),
                        responseData3 : JSON.parse(body3)
                    });
                });             
            });
        });
    });
});

Can anyone shed a bit of light on this? 任何人都可以对此有所了解吗?

You might find async.js to be useful in keeping your code clean from the "callback hell." 您可能会发现async.js有助于保持代码免受“回调地狱”的侵扰。 Specifically the parallel or series methods. 特别是并行串联方法。

For example (not tested): 例如(未测试):

async = require('async');

app.get('/profile', isLoggedIn, function(req, res) {
    async.parallel({
        records: function(callback) {
            Object.find().exec(function (err, records) {
                callback(null, records);
            });
        },
        responseData1: function(callback) {
            request('firstUrl', function(err, resp, body) {
                callback(null, JSON.parse(body));
            });
        }, 
        responseData2: function(callback) {
            request('firstUrl', function(err, resp, body) {
                callback(null, JSON.parse(body));
            });
        },
        responseData3: function(callback) {
            request('firstUrl', function(err, resp, body) {
                callback(null, JSON.parse(body));
            });
        },
    },
    function(err, results) {
        res.render('profile.ejs', {
            user : req.user,
            records : results.records,
            responseData1 : results.responseData1,
            responseData2 : results.responseData2,
            responseData3 : results.responseData3
        });
    });
});

The parallel method can also take an array of functions instead of an object, which may also be useful for you. 并行方法还可以采用函数数组代替对象,这可能对您也很有用。 Take look at the documentation. 看一下文档。

If you plan on having a variable number of URLs you fetch with request, it may make sense to refactor the code to use async.js's queue or cargo methods instead of repeating the same code for each URL you want to load. 如果您计划使用随请求获取的可变数量的URL,则可以合理地重构代码以使用async.js的queue或cargo方法,而不是对要加载的每个URL重复相同的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM