Nat*_*hat 6 javascript node.js firebase firebase-hosting firebase-realtime-database
我正在使用Firebase来托管我的nodejs应用程序并使用云功能.
使用命令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."]}}
Run Code Online (Sandbox Code Playgroud)
如何解决这个问题?
firebase.json看起来像这样: -
{
"database": {
"rules": "database.rules.json"
},
"hosting": {
"public": "public",
"rewrites": [
{
"source": "**",
"function": "app"
}
],
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"trailingSlash": true
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的文件夹结构: -
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) ;
Run Code Online (Sandbox Code Playgroud)
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
}
})
} )
}
Run Code Online (Sandbox Code Playgroud)
我找到了一个简单的解决方法:-
“/path”(在“path”之前有一个斜杠)在nodejs中有效,但在firebase中无效。
“path”(不带斜杠)在 Nodejs 和 Firebase 中都有效。
而不是action ="/postpath"
,应该是 action="postpath"
。这确保了 get 和 post 请求以相对路径方式发送到 Express 路由器,Firebase 转发到相关函数。
如果路径不是相对的,例如,如果您运行本地服务器,则action="/postpath"
解析为localhost:8000/postpath
在nodejs中工作正常,但在firebase中则不起作用,因为firebase的函数在firebase托管服务器上有自己的URL。
因此localhost:8000/postpath
甚至不会传递给我们的 Express 应用程序函数处理程序。
但是当action="postpath"
使用时,它解决了这个问题http://localhost:5001/<project-id>/us-central1/app/postpath
,现在可以完美地工作。
另外,如果您的一个或多个静态文件未得到服务(您收到 404 错误),可能是因为您<script src="/path">
在源代码开头添加了斜杠。
删除它/
即可完美运行。
归档时间: |
|
查看次数: |
878 次 |
最近记录: |