我们有一个毕业设计,我们决定用flutter.We制作一个应用程序,我们认为我们可以对来自节点js的数据使用防火墙身份验证和数据库存储,但是我找不到它的任何例子。我看到了MongoDB/Node和Express js的应用程序。这是否有可能通过使用firebase和由节点js制作的API来制作应用?
发布于 2021-12-14 19:43:03
您可以从为项目安装防火墙开始。
npm install firebase下面是Google提供的代码示例
// Get a database reference to our posts
const db = getDatabase();
const ref = db.ref('server/saving-data/fireblog/posts');
// Attach an asynchronous callback to read the data at our posts reference
ref.on('value', (snapshot) => {
console.log(snapshot.val());
}, (errorObject) => {
console.log('The read failed: ' + errorObject.name);
}); 我在我的一个项目中做的,所以建议你读一下文档。
https://firebase.google.com/docs/web/setup
更新
因此,您需要设置您的firebase项目,请按照这里的说明进行https://firebase.google.com/docs/database/web/start
现在,第二件事是如何为您的node.js速递项目添加防火墙。
const { initializeApp } = require("firebase/app");
const { getDatabase, ref, set, onValue } = require("firebase/database");
// Set the configuration for your app
// TODO: Replace with your project's config object
const firebaseConfig = {
apiKey: process.env.YOUR_VALUE_FROM_DOT_ENV_FILE,
authDomain: process.env.YOUR_VALUE_FROM_DOT_ENV_FILE,
databaseURL: process.env.YOUR_VALUE_FROM_DOT_ENV_FILE,
projectId: process.env.YOUR_VALUE_FROM_DOT_ENV_FILE,
storageBucket: process.env.YOUR_VALUE_FROM_DOT_ENV_FILE,
messagingSenderId: process.env.YOUR_VALUE_FROM_DOT_ENV_FILE,
appId: process.env.YOUR_VALUE_FROM_DOT_ENV_FILE
};
const firebase = initializeApp(firebaseConfig);
// Get a reference to the database service
const database = getDatabase(firebase);您将从firebase帐户获得所有这些东西
这里的Express部分是示例的一部分
app.post('/items', (req, res) => {
const postData = req.body;
const db = getDatabase();
const ephemeralId = Date.now().toString();
set(ref(db, 'items/' + ephemeralId), postData).then(_r => {
res.status(201).send({
success: true,
insertedId: ephemeralId,
});
}).catch(err => {
res.status(420).send(err);
});
});正如我所承诺的,这里是git https://github.com/labadze/node-express-firebase-database-api/tree/master上的完整代码
https://stackoverflow.com/questions/70350994
复制相似问题