对firebase来说真的是个新手,代码水平很低。我试图找出一个错误,自定义404没有显示,或者当重写更改时,应用程序就不起作用。
"rewrites": [
{
"source": "**",
"function": "app"
}
]
这会将所有内容重定向到应用程序,并且应用程序功能正常。但是,404.html不会显示是否尝试了错误的URL。有一个通用的";无法获取/asdfasd";
将其更改为
"rewrites": [
{
"source": "/",
"function": "app"
}
]
这将正确显示自定义404.html。
然而,有了这一点,一个函数会以404响应此配置,从而破坏应用程序。从函数/index.js:
app.post('/sendsmstoconsumer', (request, response) => {
client.messages
.create({body: request.body.message, from: '+15555555555', to: request.body.phone})
.then(message => {
response.send("Your message: " + request.body.message + " has been sent!")
});
})
我已经尝试了许多类似以下的变体,但我只是缺少了一些东西。
{
"source": "/",
"function": "app"
},
{
"source": "/sendsmstoconsumer",
"function": "sms"
}
]
change functions/index.js:
exports.sms = functions.httpsOnRequest((request, response) => {
client.messages
.create({body: request.body.message, from: '+15555555555', to: request.body.phone})
.then(message => {
response.send("Your message: " + request.body.message + " has been sent!")
});
})
如果能以正确的方式构建重写,或更改自定义404和应用程序的功能,我将不胜感激。我似乎被其中一个卡住了。
谢谢。
根据要求,编辑以添加完整的文件:
firebase.json
{
"database": {
"rules": "database.rules.json"
},
"hosting": {
"public": "public",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "**",
"function": "app"
}
]
},
"emulators": {
"functions": {
"port": 5001
},
"database": {
"port": 9000
},
"hosting": {
"port": 5000
},
"ui": {
"enabled": true
}
}
}
index.js
const functions = require('firebase-functions')
const express = require('express')
const app = express()
const accountSid = 'xxx'
const authToken = 'xxx'
const client = require('twilio')(accountSid, authToken)
const cors = require('cors')
// Enable cors for everything
// TODO: Re-enable cors at a later date
// src: https://www.npmjs.com/package/cors
app.use(cors())
app.post('/sendsmsverificationmessage', (request, response) => {
client.verify.services('xxx')
.verifications
.create({to: request.body.phone, channel: 'sms'})
.then(verification => {
response.send("Your verification code has been sent to your phone!" )
});
})
app.post('/verifyusersphonenumber', (request, response) => {
client.verify.services('xxx')
.verificationChecks
.create({to: request.body.phone, code: request.body.token})
.then(verification_check => {
response.send("Your phone has been verified!")
});
})
app.post('/sendsmstoconsumer', (request, response) => {
client.messages
.create({body: request.body.message, from: '+15555555555', to: request.body.phone})
.then(message => {
response.send("Your message: " + request.body.message + " has been sent!")
});
})
exports.app = functions.https.onRequest(app)
默认情况下,重写不会"跌倒";一旦匹配,云功能发送的响应将始终直接返回给用户。实际上有一种方法可以解决这个问题:如果您的响应主体包含X-Cascade: PASS
的标头。
在这种情况下,Firebase主机将进入默认404行为(包括自定义404.html(。您应该能够将其添加到您的Express应用程序中,如下所示:
// *AFTER* all other endpoints
app.use((req, res) => {
res.set('X-Cascade', 'PASS');
res.status(404).send('Not Found');
});
感谢Michael Bleigh的回答,我们找到了一个解决方案,但必须指定404.html文件。
app.use((req, res) => {
res.set('X-Cascade', 'PASS')
res.status(404).redirect('404.html')
})