Firebase Local Emulator Suite详解
创作时间:
作者:
@小白创作中心
Firebase Local Emulator Suite详解
引用
CSDN
1.
https://blog.csdn.net/yikezhuixun/article/details/139326637
Firebase Local Emulator Suite 是一组本地服务,可以模拟 Firebase 平台的在线服务,帮助开发者在本地环境中开发和测试应用程序,而不必依赖实时的 Firebase 项目。以下是关于 Firebase Local Emulator Suite 的详细介绍及其使用方法。
Firebase Local Emulator Suite 组件
- Authentication Emulator:模拟 Firebase Authentication,用于测试用户身份验证。
- Firestore Emulator:模拟 Cloud Firestore 数据库。
- Realtime Database Emulator:模拟 Firebase Realtime Database。
- Storage Emulator:模拟 Firebase Storage。
- Hosting Emulator:模拟 Firebase Hosting 环境。
- Functions Emulator:模拟 Cloud Functions for Firebase。
- Pub/Sub Emulator:模拟 Google Cloud Pub/Sub。
安装和使用步骤
1. 安装 Firebase CLI
首先确保安装了 Firebase CLI。如果尚未安装,请运行以下命令:
npm install -g firebase-tools
2. 初始化 Firebase 项目
在项目根目录下运行以下命令:
firebase init
选择要初始化的 Firebase 功能,并确保选择 “Emulators” 以启用本地模拟器。
3. 配置模拟器
在初始化过程中,选择你要使用的模拟器,例如 Firestore、Functions 等。初始化完成后,Firebase 会生成一个 firebase.json 配置文件,其中包含模拟器的配置。
4. 启动模拟器
在项目根目录下运行以下命令以启动所有配置的模拟器:
firebase emulators:start
你将看到类似如下的输出:
✔ All emulators ready! It is now safe to connect your app.
┌────────────────┬────────────────────────────────┐
│ Emulator │ Host:Port │
├────────────────┼────────────────────────────────┤
│ Authentication │ localhost:9099 │
│ Firestore │ localhost:8080 │
│ Functions │ localhost:5001 │
│ Hosting │ localhost:5000 │
│ Pub/Sub │ localhost:8085 │
│ Storage │ localhost:9199 │
└────────────────┴────────────────────────────────┘
┌────────────────┬────────────────────────────────┐
│ View Emulator │ http://localhost:4000 │
└────────────────┴────────────────────────────────┘
这些是本地模拟器的地址和端口,可以在浏览器中打开 http://localhost:4000 查看模拟器套件的控制面板。
5. 配置应用程序使用本地模拟器
你需要将你的应用程序配置为使用本地模拟器。例如,使用 Firebase SDK 时,可以如下配置 Firestore 和 Authentication:
import firebase from 'firebase/app';
import 'firebase/firestore';
import 'firebase/auth';
// 初始化 Firebase
const firebaseConfig = {
apiKey: "YOUR_API_KEY",
authDomain: "YOUR_PROJECT_ID.firebaseapp.com",
projectId: "YOUR_PROJECT_ID",
storageBucket: "YOUR_PROJECT_ID.appspot.com",
messagingSenderId: "YOUR_MESSAGING_SENDER_ID",
appId: "YOUR_APP_ID"
};
firebase.initializeApp(firebaseConfig);
// 使用本地模拟器
if (window.location.hostname === 'localhost') {
firebase.firestore().useEmulator('localhost', 8080);
firebase.auth().useEmulator('http://localhost:9099');
}
常见用途
- 开发和测试:在本地开发和测试 Firebase 应用程序,无需每次更改都部署到云端。
- 集成测试:使用本地模拟器进行端到端集成测试,确保所有组件正常工作。
- 离线开发:即使没有网络连接,也能继续开发和测试应用程序。
热门推荐
首次公开上甘岭影像,揭秘《我的祖国》创作始末
中科院西双版纳植物园推出并地保护新策略
揭秘版纳植物园的神秘科学营:2024雨林博物科学营
中科院西双版纳植物园举办首届珍稀植物展,百种“国保”植物亮相
西双版纳植物园摄影打卡攻略:五大绝美取景地等你来拍
吃菠菜拯救脱发危机?科学证据这样说
头皮健康大揭秘:这些小疙瘩竟然是……
头皮瘙痒:不只是身体的困扰,更是心理的枷锁
茶树精油&馥绿德雅,拯救你的“头顶危机”
甲流阳性率超99%,专家解析儿童流感防治要点
甲流咳嗽儿童饮食指南:6种营养食物助力康复
冬季燃气灶电池更换指南:7步完成,附选购建议
安全更换志高燃气灶电池:5步完成,3大注意事项
专家建议:乙肝大三阳患者生活管理六大要点
乙肝大三阳诊疗指南:HBsAg数值的意义与解读
恩替卡韦仿制药集采降价90%,乙肝患者用药负担大幅减轻
中国近8000万乙肝患者需规范管理,中西医结合助力临床治愈
大便潜血阳性:及时就医是关键
松下智能马桶盖拆装全攻略:从新手到行家
九牧智能马桶座圈拆卸指南:6步轻松完成更换
维生素C提升免疫力,冬季补充有讲究
冬季养生关键:四种维生素的多重保健功效
珠江帝景苑业委会破解业委会组建难与“力不及”
维生素C是苹果10倍,土豆5种美味做法详解
头肩顶底形态解析:股票技术分析入门
专家解析:类风湿关节炎患者的日常生活管理要点
石韦:秋季养生的明智之选
石韦:本草纲目的神奇草药
石韦:冬季呼吸道救星
孔子教你从长计议,职场决策不再难