【问题标题】:Firebase gives "path not recognized" error for post requestsFirebase 为发布请求提供“路径无法识别”错误
【发布时间】:2018-06-16 19:00:39
【问题描述】:

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

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

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

但是当我通过 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
  }
}

这是我的文件夹结构:-

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) ;

requests_controller 文件的内容(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
            }
        })

    } )

}

【问题讨论】:

  • 您的重写规则是什么让 Hosting 将您的请求代理到 Cloud Functions?
  • 我已经发布了我的 firebase.json 的内容。请帮助我
  • 你能展示你的云函数 index.js 吗?
  • 看起来 /putNPK 是 node.js 特定的路由。您可能需要将 action='/putNPK' 更改为您尝试保存到的 firebase 端点。
  • 这可能也已在stackoverflow.com/questions/44461082/… 处得到解决。

标签: javascript node.js firebase firebase-realtime-database firebase-hosting


【解决方案1】:

我找到了一个简单的解决方法:-

  • "/path"(在 'path' 之前有一个斜杠)在 nodejs 中有效,但 在 firebase 中无效

  • “路径”(不带 SLASH)在 nodejs 和 firebase 中都可以使用。

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

如果路径不是相对的,例如,如果您运行本地服务器,则 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">

删除/,它可以完美运行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-14
    • 2017-02-02
    • 1970-01-01
    • 1970-01-01
    • 2022-07-14
    • 2011-07-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多