简体   繁体   English

如何调用外部 API 然后从 hapi.js Api 返回此数据

[英]How to call an external API then return this data from an in hapi.js Api

I am new to the Hapi.js node extension.我是 Hapi.js 节点扩展的新手。

I am trying to call an external API into my server because the external API is protected with CORS and I can't call it from my front (Angular 9).我正在尝试将外部 API 调用到我的服务器中,因为外部 API 受 CORS 保护,我无法从我的前面调用它(Angular 9)。

So I set up my hapi server with routes etc and now in a route I am trying to import the external data and when the front call the route of my hapi api it show the data from the external API.所以我用路由等设置了我的 hapi 服务器,现在在一个路由中我试图导入外部数据,当前面调用我的 hapi api 的路由时,它显示来自外部 API 的数据。

I didn't find any documentation or already topics about this problem, if you could provide me with some information it would be very helpful!我没有找到有关此问题的任何文档或已有主题,如果您可以向我提供一些信息,那将非常有帮助!

(I want to do my external API call from the route solcast) (我想从路由 solcast 进行我的外部 API 调用)

This is my index.js :这是我的index.js

'use strict';

require('dotenv').config()
const Hapi = require('@hapi/hapi');

const init = async () => {

    const server = Hapi.server({
        port: 3000,
        host: 'localhost',
        routes: {
            cors: true
        }
    });

    server.route(require('./routes/base').test);
    server.route(require('./routes/solcast').solcast);

    await server.start();

    console.log('Server running on %s', server.info.uri);
};

process.on('unhandledRejection', (err) => {

    console.log(err);
    process.exit(1);
});

init();

This is my solcast.js :这是我的solcast.js

This while console.log the error:这虽然 console.log 错误:

Error: handler method did not return a value, a promise, or throw an error

And then console.log the data.然后 console.log 数据。 I assume that the data is not received when the return is done.我假设返回完成时没有收到数据。

const joi = require('@hapi/joi');
const fetch = require("node-fetch");



exports.solcast = {

    method: 'GET',
    path: '/solcasttest',
    handler: (request, h) => {
        fetch("https://linkToTheExternalApi")
        .then(response => response.json())
        .then(data => {
            console.log(data)
            return data
        })
        .catch(err => console.log(err))
        console.log(testSolcast)

    }

}

Thank you for your help, if you need any other information hit me up.感谢您的帮助,如果您需要任何其他信息,请联系我。

As the error thrown suggests, a handler in hapi.js must return a value, a promise or throw an error.正如抛出的错误所暗示的, hapi.js中的handler必须返回一个值,一个 promise 或抛出一个错误。

In your case, the handler is an asynchronous operation, so you have to return a promise.在您的情况下, handler是异步操作,因此您必须返回 promise。

As fetch already creates a promise, it is enough if you return the promise created by fetch in your handler:由于fetch已经创建了一个 promise,如果您在处理程序中返回由fetch创建的 promise 就足够了:

const fetch = require("node-fetch");

exports.solcast = {
    method: 'GET',
    path: '/solcasttest',
    handler: (request, h) => {
        return fetch("https://linkToTheExternalApi")
            .then(response => response.json())
            .then(data => {
                console.log(data)
                return data
            })
            .catch(err => console.log(err));
    }
}

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

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