简体   繁体   English

Firebase和外部API 404(Firebase函数)

[英]Firebase and external API 404 (firebase functions)

I'm trying to get external API with Firebase functions and display information in an Angular app. 我正在尝试使用Firebase函数获取外部API,并在Angular应用中显示信息。 The angular app is on firebase hosting. Angular应用位于Firebase托管上。 Billing is enabled. 计费已启用。 On localhost, it's working but on firebase cannot get data. 在localhost上,它可以工作,但是在Firebase上无法获取数据。

const functions = require('firebase-functions');

const   express       = require('express')
  , https         = require('https')
  , bodyParser    = require('body-parser')
  , app           = express()
  , cors          = require('cors');


app.use(cors());
app.use(bodyParser.json());

const options = {
  host: 'api.clashofstats.com',
  port: 443,
  path: '/clans/YUPJGL2R/',
  headers: {
    accept: "application/json",
  }
};

app.get("/clan",(req, res) => {
  const request = https.request(options, (response) =>{
    let httpResult = '';
    response.on('data',  (chunk) => {
      httpResult += chunk;
    });
    response.on('end', () => {
      res.send(httpResult);
    });
  });

  request.on('error',(e) => {
    console.log('problem with request: ' + e.message);
  });

  request.end();

});

exports.app = functions.https.onRequest(app);

console log 控制台日志

ERROR e {headers: t, status: 200, statusText: "OK", url: "https://united-clan.cz/clan", ok: false, …}

firebase log Firebase日志

Function execution took 10 ms, finished with status code: 404

network 网络

Request URL: https://united-clan.cz/clan
Request Method: GET
Status Code: 200  (from disk cache)
Remote Address: 151.101.65.195:443
Referrer Policy: no-referrer-when-downgrade

https://us-central1-ucweb-224315.cloudfunctions.net/app 

Cannot GET null

You'll need to add a trailing slash to the url - your issue sounds similar to this one: 您需要在网址后添加斜杠-您的问题听起来与此类似:

https://github.com/firebase/functions-samples/issues/101 https://github.com/firebase/functions-samples/issues/101

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

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