gpt4 book ai didi

javascript - 如何在 Firebase 托管和函数中配置重写和映射前缀动态路由路径?

转载 作者:太空宇宙 更新时间:2023-11-04 01:46:21 27 4
gpt4 key购买 nike

我启用了 firebase 页面和功能。我想要一个像下面这样的 api 路由:

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

我确实遵循了 Firebase 团队的指南 https://youtu.be/LOeioOKUKI8 (动态 Node 应用程序),他在其中映射或 Hook 路径https://mmydomain.com/timestamp 到 firebase 函数“exports.app”,其中他有 /timestamp 快速路由路径。他的代码类似于下面的代码:

functions/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

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

现在我的目标有点相同,但我想在路径前加上“api”前缀,这样我就可以拥有上面列出的 api 路径。我尝试先工作 https://mmydomain.com/api/generatorPerson 使用下面的 setup/config 和 firebase config 函数。它部署没有错误,但我收到 404 错误。

functions/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

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

我使用了模式/api/**,因为我希望所有子路由都以 api 为前缀并将它们映射到exports.app。我的代码有什么问题?还是我的前缀错误?

最佳答案

尝试

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

/api 已经映射到 Express 应用

关于javascript - 如何在 Firebase 托管和函数中配置重写和映射前缀动态路由路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51311432/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com