在使用node几年之后,我可以说目录/文件结构 没有 约定。但是,大多数(专业)快递应用程序都使用类似以下的设置:
/ /bin - scripts, helpers, binaries /lib - your application /config - your configuration /public - your public files /test - your tests
使用此设置的示例是nodejs-starter。
我个人将此设置更改为:
/ /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风格的目录结构更好地匹配(而前者将其混为一谈)。
我也喜欢这种模式来分隔文件:
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的解决方案。附近有一个使用此设置的真实示例。
更新(文件名):
关于文件名最常见的是 短的 , 小写的 文件名。如果您的文件只能用两个词来描述,那么大多数Javascript项目都使用下划线作为分隔符。
更新(变量):
关于变量,相同的“规则”适用于文件名。但是,原型或类应使用 camelCase 。
更新(样式指南):
- https://github.com/feross/standard
- https://github.com/airbnb/javascript



