从REST Api(Node.js)中调用REST Api

时间:2022-10-16 13:17:41

This might be a silly question but I'm quite new to Node/REST and could not find a answer.

这可能是一个愚蠢的问题,但我对Node / REST很新,但找不到答案。

Say there is a Request A that asks for Object A ('../student/:studentId'). And there is another Request B that asks for Object B ('.../lecture/:lectureid'). Now Object B will contain some information about the lecture but also the students that attend the lecture.

假设有一个请求A要求对象A('../student/:studentId')。还有另一个请求B要求对象B('... /讲座/:讲座')。现在,对象B将包含有关讲座的一些信息,以及参加讲座的学生。

Now I can think of three possbile ways to assemble Object B:

现在我可以想到三种可能的方法来组装对象B:

1.: Call Request A several times from within the processing of Resquest B

1:在Resquest B的处理中多次呼叫请求

2.: Copy and paste the code from the processing of Request A

2:复制并粘贴处理请求A的代码

3.: Create an object that accesses the database and attach it to all request queries:

3。:创建一个访问数据库并将其附加到所有请求查询的对象:

    var dbAccessObject = require('./dbAccess');

    app.use(function (req,res,next){
      req.dbAccessObject = dbAccessObject;
      next();
    )};

Which option would you choose? Or is there another, better way?

你会选择哪个选项?或者还有另一种更好的方法吗?

1 个解决方案

#1


2  

You don't need to forward requests to yourself. You can handle it within your own code by structuring it nicely. Let's say you have two API routes:

您无需将请求转发给自己。您可以通过很好地构建它来在自己的代码中处理它。假设您有两条API路线:

/a: runA();
/b: runB();

And you would like /c to return the result of both /a and /b as if two calls were made. If you have broken your logic down to runA() and runB() as above, then /c becomes runC():

并且你希望/ c返回/ a和/ b的结果,就像两次调用一样。如果您将逻辑分解为runA()和runB(),则/ c变为runC():

return {
  a: runA(),
  b: runB()
}

This is simple when writing synchronous code, but asynchronous code is a bit more difficult because runB() could return before runA(); you need to know when they are both finished. I recommend using the async library for this as a quick win: http://www.informit.com/articles/article.aspx?p=2265406&seqNum=2

编写同步代码时这很简单,但异步代码有点困难,因为runB()可以在runA()之前返回;你需要知道它们何时完成。我建议使用异步库作为快速获胜:http://www.informit.com/articles/article.aspx?p = 2265406&seqNum = 2

#1


2  

You don't need to forward requests to yourself. You can handle it within your own code by structuring it nicely. Let's say you have two API routes:

您无需将请求转发给自己。您可以通过很好地构建它来在自己的代码中处理它。假设您有两条API路线:

/a: runA();
/b: runB();

And you would like /c to return the result of both /a and /b as if two calls were made. If you have broken your logic down to runA() and runB() as above, then /c becomes runC():

并且你希望/ c返回/ a和/ b的结果,就像两次调用一样。如果您将逻辑分解为runA()和runB(),则/ c变为runC():

return {
  a: runA(),
  b: runB()
}

This is simple when writing synchronous code, but asynchronous code is a bit more difficult because runB() could return before runA(); you need to know when they are both finished. I recommend using the async library for this as a quick win: http://www.informit.com/articles/article.aspx?p=2265406&seqNum=2

编写同步代码时这很简单,但异步代码有点困难,因为runB()可以在runA()之前返回;你需要知道它们何时完成。我建议使用异步库作为快速获胜:http://www.informit.com/articles/article.aspx?p = 2265406&seqNum = 2