简体   繁体   English

Node.js和Express中的数据库配置文件应该放在哪里?

[英]Where should be located a database config file in Node.js and Express?

I'm now learning Node.js and Express, and want to use mysql module to render multiple unique pages, so instead of writing out var connection=mysql.createConnection({user:'root', password: 'root'...etc}) line on every file located under routes directory, I'm sure it's better off to just call my own mysql config file on each routing file. 我现在正在学习Node.js和Express,并且想要使用mysql模块来呈现多个唯一页面,因此var connection=mysql.createConnection({user:'root', password: 'root'...etc})写出var connection=mysql.createConnection({user:'root', password: 'root'...etc})行放在routes目录下的每个文件上,我相信最好在每个路由文件上调用我自己的mysql配置文件。

However, where should I put the config file on Express hierarchy and how can I call the file from within each routing file? 但是,我应该将配置文件放在Express层次结构的哪个位置,如何在每个路由文件中调用该文件? I know all images, style sheets, and javascript files should be located within each specific directory under public directory, but I don't know where to put all the other files that are intended to be accessed from routing files. 我知道所有图像,样式表和javascript文件都应位于public目录下的每个特定目录中,但是我不知道将所有其他文件(打算从路由文件访问)放在何处。

I also want to know whether all of my files, ranging from main app.js to files under routes directory to files under public directory, etc... can be found by users once I publicize this web application on the Web. 我还想知道我在Web上公开此Web应用程序后,是否可以找到我的所有文件,从主要app.jsroutes目录下的文件,再到public目录下的文件,等等。 If it's the case, then I think I should not put database config file on directories that clients can access to...right? 如果是这种情况,那么我认为我不应该将数据库配置文件放在客户端可以访问的目录上...对吗? In other words, I want to make sure which files can be accessed to by clients and which cannot in order to avoid security attacks. 换句话说,我想确保客户端可以访问哪些文件,而哪些文件不能避免以避免安全攻击。

To answer your first question "Where to put the config file?": This is a little bit personnal. 要回答您的第一个问题“配置文件在哪里?”:这有点个人。 Put it to the root of your application. 将其放到应用程序的根目录。

config.js: config.js:

module.exports = {
  database:{
    host: ""
    user: "..."
  }
}

then you include it in you app.js: 然后将其包含在app.js中:

app.js: app.js:

...
config = require("./config");
db = config.database;
var connection=mysql.createConnection({user:db.user, ...})

Note that you might want two config file, one in you version control and one private to the machine. 请注意,您可能需要两个配置文件,一个在版本控制中,另一个在计算机专用。 Different developers might have different database access for example. 例如,不同的开发人员可能具有不同的数据库访问权限。 But I don't think you have to worry about that for now. 但是我认为您现在不必为此担心。

For your second question "Are all my files public?": No, only the file you specify as static (with the express middleware) will be served. 对于第二个问题“我的所有文件是否公开吗?”:否,仅提供您指定为静态的文件(使用快速中间件)。

app.js: app.js:

...
// Exposes the public folder
app.use(express.static(__dirname + '/public'));

You can put them wherever you want. 您可以将它们放在任意位置。 I usually create a /conf directory right off the project root and put config files in there, in JSON format. 我通常在项目根目录附近创建一个/conf目录,并将配置文件以JSON格式放在其中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM