【问题标题】:Firebase and external API 404 (firebase functions)Firebase 和外部 API 404(firebase 函数)
【发布时间】:2019-05-26 11:25:40
【问题描述】:

我正在尝试使用 Firebase 函数获取外部 API 并在 Angular 应用中显示信息。角度应用程序位于 Firebase 托管上。计费已启用。在 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);

控制台日志

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

firebase 日志

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

网络

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

【问题讨论】:

  • 这段代码在./functions/index.js里面吗?

标签: angular api firebase express


【解决方案1】:

您需要在 url 中添加一个斜杠 - 您的问题与此类似:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-27
    • 2016-10-08
    • 2020-09-12
    • 1970-01-01
    • 1970-01-01
    • 2021-03-15
    • 2020-09-06
    • 1970-01-01
    相关资源
    最近更新 更多