简体   繁体   English

nodejs调用API并在响应中返回

[英]nodejs call an API and return that in a response

I am very new to nodejs, and I am using restify to create routes, and one of the routes, I call another API, whose response I want to send back. 我对nodejs非常陌生,我正在使用restify来创建路由,并且其中一个路由调用了另一个API,我想将其响应发送回去。

server.get({path:'/admin'},
            function respond(req, res, next){
            var options = { method: 'GET',
                  url: argv.OKTA + "/api/v1/apps/" + argv.OKTA_APP_ID,
                  headers:
                   { authorization: argv.OKTA_API,
                     'content-type': 'application/json',
                     accept: 'application/json' } };

            request(options, function (error, response, body) {
                if (error) throw new Error(error);

                res.send(200, body);
                return next();
            });

});

So, if a GET is done on the /admin path, it should go and call another URL, and get the response back and use that as a response back to the /admin 因此,如果在/ admin路径上完成GET,则它应该去调用另一个URL,然后获取响应并将其用作对/ admin的响应

Right now, I get this instead: 现在,我得到的是:

{"code":"ResourceNotFound","message":"/admin does not exist"}

由于您正在发送响应,因此您不应调用next() (它将调用下一个中间件,该中间件最终将在您的404中间件处)。

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

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