无法访问调用Firebase http端点

时间:2019-06-23 01:16:21

标签: node.js firebase express google-cloud-functions

我正在编写一个Firebase函数,并试图使其用作HTTP端点。但是,当我尝试访问时,出现错误“您的客户端没有从该服务器获取URL / getDetails的权限。”

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

const cors = require('cors');

var express = require('express')
var app = express();


'use strict';
app.use(cors);
app.get('/getDetails', function (req, res) {

res.writeHead(200);
var jsonObj = {
    fName: 'Karthik',
    lName: 'Mannepalli'
};
res.end(JSON.stringify(jsonObj));
});

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

我希望输出{“ fName”:“ Karthik”,“ lName”:“ Mannepalli”},但我得到的是错误:禁止

但是下面的代码给了我正确的输出。在下面的代码中,我没有使用快递

exports.getDetails = functions.https.onRequest(async (req, res) => {
const original = req.query.text;
res.writeHead(200);
var jsonObj = {
    fName: 'Karthik',
    lName: 'Mannepalli'
};
res.end(JSON.stringify(jsonObj));
});

2 个答案:

答案 0 :(得分:1)

请参见https://firebase.google.com/docs/functions/http-events

    exports.date = functions.https.onRequest((req, res) => {
      // ...
    });
  

例如,调用date()的URL如下所示:

     

https://us-central1-<project-id>.cloudfunctions.net/date

    const express = require('express');
    const cors = require('cors');

    const app = express();

    // Automatically allow cross-origin requests
    app.use(cors({ origin: true }));

    // Add middleware to authenticate requests
    app.use(myMiddleware);

    // build multiple CRUD interfaces:
    app.get('/:id', (req, res) => res.send(Widgets.getById(req.params.id)));
    app.post('/', (req, res) => res.send(Widgets.create()));
    app.put('/:id', (req, res) => res.send(Widgets.update(req.params.id, req.body)));
    app.delete('/:id', (req, res) => res.send(Widgets.delete(req.params.id)));
    app.get('/', (req, res) => res.send(Widgets.list()));

    // Expose Express API as a single Cloud Function:
    exports.widgets = functions.https.onRequest(app);
  

例如,在上面的Express应用示例中用于调用getter的URL如下所示:

     

https://us-central1-<project-id>.cloudfunctions.net/widgets/<id>

因此,对于exports.app = functions.https.onRequest(app);,获取URL为/app/getDetails。不是/getDetails

对于exports.getDetails = functions.https.onRequest(async (req, res),获取的URL为/getDetails

答案 1 :(得分:0)

您的云功能代码对我来说一点都不熟悉。我不确定您使用的是哪个教程或文档,但是应该像这样简单...

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

exports.SomeFunction = functions.https.onRequest((req, res) => {
   //Your function code
   res.status(200).end();
});

尝试该代码,如果还有其他错误,请通知我。