[英]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路由:
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: 我设法通过以下方式解决了该问题:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.