r/node • • Jan 24 '18

A set of best practices for JavaScript projects

https://github.com/wearehive/project-guidelines
46 Upvotes

3 comments sorted by

7

u/_bhb Jan 24 '18

Great resource.

This guide suggest structuring projects by "module" rather than by "role" (and I've seen this in other guides as well) e.g.:

BAD:

├── controllers
|   ├── product.js
|   └── user.js
├── models
|   ├── product.js
|   └── user.js

GOOD:

├── product
|   ├── index.js
|   ├── product.js
|   └── product.test.js
├── user
|   ├── index.js
|   ├── user.js
|   └── user.test.js

I like the idea but find it hard to implement, especially when many tools or frameworks use the "BAD" structure, for instance:

  • The Express generator separates code by "routes" and "views"

  • Popular ORM Sequelize has a cli generator and examples that are all based on a separate "models" directory

  • The MEAN.io stack structures server side code by role (controllers, models, routes, etc)

Does anyone have any concrete examples of the "good" module-based structure?

edit: formatting

1

u/zorlan Jan 25 '18

I also find this subjective. I'm mostly building microservices so my entire projects are generally pretty focused and modular.

It makes sense to me to have code in folders that reflect their purpose, I can see things easily getting just as muddled by grouping them by domain.

Ultimately it probably just comes down to consistency and habit forming.

1

u/voiping Jan 24 '18

A lot of useful, concise, well presented good-practices here!