我开始学习 NodeJS 和 Mongodb,现在我想在 GitHub 上上传我的项目,但不知道如何隐藏连接数据库的链接。应该这样做吗?
mongoose.connect('mongodb+srv://howToHideThisInfo?:[电子邮件受保护]/database?retryWrites=true&w=majority')
I am started learn NodeJS and Mongodb and now I want to upload my project on GitHub, but don`t know how to hide link for connect to database. And should it be done?
mongoose.connect('mongodb+srv://howToHideThisInfo?:[email protected]/database?retryWrites=true&w=majority')
发布评论
评论(1)
您可以将 URI 放入文件中,然后将该文件添加到
.gitignore
中,并且可以使用process.env
变量,更多信息请参见 Process.env。您可以在启动节点应用程序时将变量传递为USER_ID=239482 USER_KEY=foobar node app.js
,并将要使用它们的代码更新为process.env.USER_ID
>。所以你的 URI 代码将如下所示:You can put the URI in a file and then add that file to
.gitignore
and you can useprocess.env
variables, more on them here Process.env. You can pass the variables when starting the node app asUSER_ID=239482 USER_KEY=foobar node app.js
and update the code where you want to use them asprocess.env.USER_ID
. So your URI code would look like so: