My recommended structure is to put everything except server.js
in lib
directory so all your app is lib/
plus server.js
- everything else is package.jsonpackage.json
, dependencies in node_modules
(created on npm install
, not in the repo), .gitignore
, config files for Travis, Circle, Heroku or whatever service you're using, some README.md
and things like that.
Now, server.js
is just bare minimum code that requires lib/app
:
where logger
is some higher lever logger like Winston or something like that.
That's it. Now, lib/app.js
is minimum code that loads the middleware like body parsers etc., creates the express app and sets the variables for port and name and then uses a router that is exported by lib/routes
:
Then I would add top level spec
or test
or tests
directory where all of the tests go. The tests can require your lib/app
to run the tests on it with no need to listen on actual TCP ports - those will test your routes with actual controllers. Other tests will require lib/util
and run some unit tests on your utilities etc. Make sure to use a tool like istanbul
or nyc
to calculate the test coverage.
The database schemas and data models would go to lib/schemas
and lib/models
, womesome utility helpers in lib/util
, some config loading code in lib/config
etc.
README.md
LICENSE.md
package.json
server.js
lib/app.js
lib/routes.js
lib/controllers.js
lib/config.js
etc. and easily convert all of the xxx.js
file into xxx/index.js
with entire folder of smaller xxx/*.js
files as needed.