Node.js项目文件的命名约定&资料夹 [英] Node.js project naming conventions for files & folders
问题描述
大型Node.js项目中文件和文件夹的命名约定是什么?
What are the naming conventions for files and folders in a large Node.js project?
我该使用大写字母,camelCase还是下划线?
Should I capitalize, camelCase, or under-score?
即.这被认为是有效的吗?
Ie. is this considered valid?
project-name
app
controllers
someThings.js
users.js
models
someThing.js
user.js
views
some-things
index.jade
users
logIn.jade
signUp.jade
...
推荐答案
在使用node几年后,我可以说目录/文件结构没有 no 约定.但是,大多数(专业)快递应用程序都使用类似以下的设置:
After some years with node, I can say that there are no conventions for the directory/file structure. However most (professional) express applications use a setup like:
/
/bin - scripts, helpers, binaries
/lib - your application
/config - your configuration
/public - your public files
/test - your tests
使用此设置的示例是 nodejs-starter .
我个人将此设置更改为:
I personally changed this setup to:
/
/etc - contains configuration
/app - front-end javascript files
/config - loads config
/models - loads models
/bin - helper scripts
/lib - back-end express files
/config - loads config to app.settings
/models - loads mongoose models
/routes - sets up app.get('..')...
/srv - contains public files
/usr - contains templates
/test - contains test files
在我看来,后者与Unix风格的目录结构更好地匹配(而前者将其混为一谈).
In my opinion, the latter matches better with the Unix-style directory structure (whereas the former mixes this up a bit).
我也喜欢这种模式来分隔文件:
I also like this pattern to separate files:
lib/index.js
var http = require('http');
var express = require('express');
var app = express();
app.server = http.createServer(app);
require('./config')(app);
require('./models')(app);
require('./routes')(app);
app.server.listen(app.settings.port);
module.exports = app;
lib/static/index.js
var express = require('express');
module.exports = function(app) {
app.use(express.static(app.settings.static.path));
};
这允许整洁地将所有源代码解耦,而不必理会依赖项.一个很好的解决讨厌的Javascript的解决方案.一个真实的示例是附近,它使用此设置.
This allows decoupling neatly all source code without having to bother dependencies. A really good solution for fighting nasty Javascript. A real-world example is nearby which uses this setup.
更新(文件名):
关于最常见的文件名是 short ,小写文件名.如果您的文件只能用两个词来描述,那么大多数JavaScript项目都使用下划线作为分隔符.
Regarding filenames most common are short, lowercase filenames. If your file can only be described with two words most JavaScript projects use an underscore as the delimiter.
更新(变量):
关于变量,相同的规则"适用于文件名.但是,原型或类应使用 camelCase .
Regarding variables, the same "rules" apply as for filenames. Prototypes or classes, however, should use camelCase.
更新(样式指南):
- https://github.com/feross/standard
- https://github.com/airbnb/javascript
这篇关于Node.js项目文件的命名约定&资料夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!