简体   繁体   English

如何在Firebase托管和功能中配置重写和映射前缀的动态路由路径?

[英]How do I configure the rewrite and map prefixed dynamic route path in firebase hosting and functions?

I have firebase page and functions enabled. 我启用了Firebase页面并启用了功能。 I want to have a api routes like the following: 我想拥有如下所示的api路由:

  1. https://mmydomain.com/api/generatorPerson https://mmydomain.com/api/generatorPerson
  2. https://mmydomain.com/api/generateLead https://mmydomain.com/api/generateLead
  3. https://mmydomain.com/api/and_so_on https://mmydomain.com/api/and_so_on

I did follow this guide from Firebase team https://youtu.be/LOeioOKUKI8 (Dynamic Node apps) in which he mapped or hooked the path https://mmydomain.com/timestamp to the firebase function 'exports.app' where he has /timestamp express route path. 我确实遵循了Firebase团队https://youtu.be/LOeioOKUKI8 (动态节点应用程序)的指南,在该指南中,他将https://mmydomain.com/timestamp映射或链接到了Firebase函数“ exports.app”具有/ timestamp表示路由路径。 His code is something like the one below: 他的代码类似于以下代码:

functions/index.json 函数/ index.json

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

const app = express();

app.get('/timestamp', (request, response) =>{
    response.send('${Date.now()}');
});

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

public/firebase.json public / firebase.json

{
  "hosting": {
    "public": "public",
    "rewrites": [
      {
        "source": "/timestamp",
        "function": "app"
      }
    ]
  }
}

Now my goal is kinda the same but I want to prefix the path with 'api' so that I can have api paths that I listed above. 现在我的目标有点相同,但是我想在路径前加上“ api”前缀,这样我就可以获得上面列出的api路径。 I tried working first the https://mmydomain.com/api/generatorPerson using the functions setup/config and firebase config below. 我首先尝试使用下面的功能setup / config和firebase config来工作https://mmydomain.com/api/generatorPerson It deploys with no error but I get 404 error. 它部署没有错误,但出现404错误。

functions/index.json 函数/ index.json

const faker = require('faker');
const express = require('express');
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

const app = express();

app.get('/api/generatePerson', (request, response) =>{
    let randomName = faker.name.findName();
    let randomEmail = faker.internet.email();
    let randomPhoneNumber = faker.phone.phoneNumber();
    let randomBirthDay = faker.date.past();
    let randomAddress = faker.address.streetAddress();
    let randomJobTitle = faker.name.jobTitle();
    let randomJobDescription = faker.name.jobDescriptor();
    let randomCompany = faker.company.companyName();

    let person = {
        name: randomName,
        email: randomEmail,
        phoneNumber: randomPhoneNumber,
        birthday: randomBirthDay,
        address: randomAddress,
        work: {
            jobTitle: randomJobTitle,
            jobDescription: randomJobDescription,
            company: randomCompany
        }
    };

    response.send(person);
});

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

public/firebase.json public / firebase.json

{
  "hosting": {
    "public": "public",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "rewrites": [
      {
        "source": "/api/**",
        "function": "app"
      }
    ]
  },
  "functions": {
    "predeploy": [
      "npm --prefix \"%RESOURCE_DIR%\" run lint"
    ]
  }
}

I used pattern /api/** since I want all the sub routes be prefixed with api and mapped them to the exports.app . 我希望使用/api/**模式,因为我希望所有子路由都以api作为前缀并将它们映射到exports.app What's the problem of my code? 我的代码有什么问题? Or am I doing the prefixing wrong? 还是我做的前缀错误?

try 尝试

app.get('/generatePerson', (request, response) =>{

/api is already mapped to the express app / api已经映射到Express应用

I managed to resolved the problem by: 我设法通过以下方式解决了该问题:

  1. Cleaned the firebase.json with any comment artifacts. 使用任何注释工件清理了firebase.json。
  2. Deploy both hosting and functions. 部署托管和功能。
  3. If the problem still there wait a couple of minutes or refresh the http call of your API. 如果问题仍然存在,请等待几分钟或刷新API的http调用。 After that it will work. 之后,它将起作用。

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

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