如何在create-react-app中隐藏API密钥? [英] How do I hide API key in create-react-app?
本文介绍了如何在create-react-app中隐藏API密钥?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在create-react-app中制作了一个天气应用程序。我如何隐藏API密钥以便我可以提交给GitHub?
现在关键在App.js中:
const API_KEY =123456;
解决方案
详细说明Arup Rakshit的评论,首先,您应该在src文件夹外创建.env文件。
然后,添加
REACT_APP_WEATHER_API_KEY = 123456
在提交之前,您应该排除这个.env文件,以便找到.gitignore文件并添加.env。
现在您可以自由前往。
不要忘记在.gitignore文件中添加.env。
I made a weather app in create-react-app. How do I hide the API key so that I can commit to GitHub?
Right now the key is in App.js: const API_KEY = "123456";
解决方案
To elaborate Arup Rakshit's comment,
First, you should make .env file outside of your src folder.
Then, add
REACT_APP_WEATHER_API_KEY=123456
Before commit, you should exclude this .env file so find .gitignore file and add .env.
Now you're free to go.
Don't forget to add .env in .gitignore file.
这篇关于如何在create-react-app中隐藏API密钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文