如何在 firebase 托管和功能中配置重写和映射前缀动态路由路径?
How do I configure the rewrite and map prefixed dynamic route path in firebase hosting and functions?
我启用了 firebase 页面和功能。我想要一个 api 路线,如下所示:
- https://mmydomain.com/api/generatorPerson
- https://mmydomain.com/api/generateLead
- https://mmydomain.com/api/and_so_on
我确实遵循了 Firebase 团队 https://youtu.be/LOeioOKUKI8 (Dynamic Node apps) in which he mapped or hooked the path 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 路径。我尝试首先使用下面的函数 setup/config 和 firebase 配置来处理 https://mmydomain.com/api/generatorPerson。它部署没有错误,但我收到 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 应用
我通过以下方式解决了问题:
- 清理了 firebase.json 的任何评论工件。
- 同时部署托管和函数。
- 如果问题仍然存在,请等待几分钟或刷新 API 的 http 调用。之后就可以了。
我启用了 firebase 页面和功能。我想要一个 api 路线,如下所示:
- https://mmydomain.com/api/generatorPerson
- https://mmydomain.com/api/generateLead
- https://mmydomain.com/api/and_so_on
我确实遵循了 Firebase 团队 https://youtu.be/LOeioOKUKI8 (Dynamic Node apps) in which he mapped or hooked the path 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 路径。我尝试首先使用下面的函数 setup/config 和 firebase 配置来处理 https://mmydomain.com/api/generatorPerson。它部署没有错误,但我收到 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 应用
我通过以下方式解决了问题:
- 清理了 firebase.json 的任何评论工件。
- 同时部署托管和函数。
- 如果问题仍然存在,请等待几分钟或刷新 API 的 http 调用。之后就可以了。