Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js project naming conventions for files & folders

What are the naming conventions for files and folders in a large Node.js project?

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     ... 
like image 766
Rudiger Avatar asked Sep 20 '13 23:09

Rudiger


People also ask

What is the best naming convention for files?

File naming best practices:Avoid special characters or spaces in a file name. Use capitals and underscores instead of periods or spaces or slashes. Use date format ISO 8601: YYYYMMDD.

What is the naming style convention in JS?

Naming ConventionsVariable and function names written as camelCase. Global variables written in UPPERCASE (We don't, but it's quite common) Constants (like PI) written in UPPERCASE.

Should Javascript files be capitalized?

File names must be all lowercase and may include underscores ( _ ) or dashes ( - ), but no additional punctuation. Follow the convention that your project uses.


1 Answers

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 

An example which uses this setup is 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 

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));  }; 

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.

Update (filenames):

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.

Update (variables):

Regarding variables, the same "rules" apply as for filenames. Prototypes or classes, however, should use camelCase.

Update (styleguides):

  • https://github.com/feross/standard
  • https://github.com/airbnb/javascript
like image 158
bodokaiser Avatar answered Oct 17 '22 04:10

bodokaiser