gpt4 book ai didi

javascript - Firebase 为发布请求提供 "path not recognized"错误

转载 作者:数据小太阳 更新时间:2023-10-29 05:28:23 24 4
gpt4 key购买 nike

我正在使用 Firebase 来托管我的 nodejs 应用程序并正在使用 Cloud Functions。

使用命令 firebase serve --only functions,hosting 我正在部署我的应用程序。

我有一个带有 action="/putNPK" 的表单,并且在从 Node 运行时完美运行。

但是当我通过 firebase 提供它时,我在提交表单时遇到了这个错误。

{"error":{"code":404,"status":"NOT_FOUND","message":"/putNPK is not a recognized path.","errors":["/putNPK is not a recognized path."]}}

如何解决这个问题?

firebase.json 看起来像这样:-

{
"database": {
"rules": "database.rules.json"
},
"hosting": {
"public": "public",
"rewrites": [
{
"source": "**",
"function": "app"
}
],
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"trailingSlash": true
}
}

这是我的文件夹结构:-

enter image description here

Contents of index.js :-

    const admin = require("firebase-admin") ; 
const express = require("express") ;
const app = require("express")() ;
const bodyparser = require("body-parser") ;
const functions = require("firebase-functions") ;
const request_controller = require("./requests_controller") ;



app.use(express.static('../public/assets/')) ;
app.use(express.static('../public/')) ;

request_controller(app) ;

app.use((req ,res , next)=>{
res.status(404).redirect('404.html') ;
})

app.listen(8000) ;

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

COntents of requests_controller file (module imported in index.js) :-

    const admin = require("firebase-admin") ; 
const bodyparser = require("body-parser") ;
const app = require("express")() ;
const urlencodedParser =bodyparser.urlencoded({extended : true}) ;
const posthandler = require("posthandler") ;
const gethandler = require("gethandler") ;


var serviceAccount = require("C:/Users/Natesh/Documents/raita-mitra-2018-firebase-adminsdk (acnt nateshmbhat1).json");


admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://raita-mitra-2018.firebaseio.com"
});



//Validates POST request body and checks if the request contains all the keys('strings') in the array sent as keys argument
function validatePostBody(req , res , keys ){
for(i in keys){
if(!(keys[i] in req.body))
{
console.log("invalid post request returning ! ") ;
return false ;
}
}
return true ;
}



module.exports = function Handle_requests(app)
{
console.log('Request Handler started ! ') ;

app.get('/' , (req , res)=>{
res.sendFile(__dirname + '/index.html') ;
})

app.get('/home' , (req , res)=>{
res.sendFile(__dirname + '/index.html') ;
})


app.post('/putNPK', urlencodedParser ,(req , res)=>{
if(!validatePostBody(req , res , ['fertilizer' ,'crop' , 'nitrogen' , 'phone' , 'phosphorus' , 'potassium'])) return ;

ref = admin.database().ref('/users/' + req.body.phone) ;
ref.set(req.body) ;
console.log("Added to firebase database") ;
res.status(200).redirect('/') ;

admin.messaging().sendToTopic('global' , {
notification : {
title : 'Farmer Project' ,
body : 'notification body'
} ,
data : {
nitrogen : req.body.nitrogen
}
})

} )

}

最佳答案

我找到了一个简单的修复方法:-

  • "/path" (with a slash before 'path') works in nodejs but not in firebase.

  • "path" (without the SLASH) works both in nodejs and firebase.

而不是 action ="/postpath" ,应该是action="postpath" .这确保了 get 和 post 请求以 firebase 转发给相关函数的相对路径方式到达 express 路由器。

如果路径不是相对的,例如,如果你运行本地服务器,那么 action="/postpath"解析为 localhost:8000/postpath这在 nodejs 中工作正常,但在 firebase 中它不起作用,因为 firebase 的函数在 firebase 托管服务器上有自己的 URL。

因此localhost:8000/postpath甚至不会传递给我们的快速应用程序函数处理程序。

但是当action="postpath"使用 ,它将此解析为 http://localhost:5001/<project-id>/us-central1/app/postpath 现在完美运行。


编辑 1:-

此外,如果您的一个或多个静态文件没有得到服务(您收到 404 错误),可能是因为您包含了 <script src="/path">在源的开头有一个斜线。

删除 /它完美地工作。

关于javascript - Firebase 为发布请求提供 "path not recognized"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48139457/

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