firebase云功能http请求无法正常工作?
当我在Postman上击中API时,我的云功能API无法正常工作,没有错误,只需在Postman上获得404即可。附加了代码和屏幕截图。
index.js
const functions = require("firebase-functions");
const admin = require("firebase-admin");
var serviceAccount = require("./key.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount)
});
const express = require("express");
const cors = require("cors");
// main app
const app = express();
app.use(cors({ origin: true }))
// main database refference
const db = admin.firestore();
// Routes test
app.get("/", (req, res) => {
return res.status(200).send("test api ")
})
// create
app.post("api/create", async (req, res) => {
console.log(req)
try {
console.log(req)
await db.collection(useDetails).doc(`/${Date.now()}/`).create({
id: Date.now(),
name: req.body.name,
mobile: req.body.mobile,
add: req.body.add
})
return res.status(200).send({ status: "Sucess", msg: "Data Saved" });
} catch (error) {
console.log(error)
return res.status(200).send({ status: "Failed", msg: "Data not Saved" });
}
})
exports.app = functions.https.onRequest(app)
My cloud functions API is not working properly when I hit api on postman, there are no error, just getting 404 on postman. Code and screenshots are attached.
index.js
const functions = require("firebase-functions");
const admin = require("firebase-admin");
var serviceAccount = require("./key.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount)
});
const express = require("express");
const cors = require("cors");
// main app
const app = express();
app.use(cors({ origin: true }))
// main database refference
const db = admin.firestore();
// Routes test
app.get("/", (req, res) => {
return res.status(200).send("test api ")
})
// create
app.post("api/create", async (req, res) => {
console.log(req)
try {
console.log(req)
await db.collection(useDetails).doc(`/${Date.now()}/`).create({
id: Date.now(),
name: req.body.name,
mobile: req.body.mobile,
add: req.body.add
})
return res.status(200).send({ status: "Sucess", msg: "Data Saved" });
} catch (error) {
console.log(error)
return res.status(200).send({ status: "Failed", msg: "Data not Saved" });
}
})
exports.app = functions.https.onRequest(app)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 /启动API地址
start api address with /