Skip to main content
added 194 characters in body
Source Link
rsp
  • 111.7k
  • 31
  • 209
  • 183

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.

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.json, dependencies in node_modules, gitignore, config files for Travis, Circle and things like that.

Now, server.js is minimum code that requires lib/app:

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.

The database schemas and data models would go to lib/schemas and lib/models, wome utility helpers in lib/util, some config loading code in lib/config etc.

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 files as needed.

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.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 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, some 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.

added 93 characters in body
Source Link
rsp
  • 111.7k
  • 31
  • 209
  • 183

I would recommend exporting a more specific router in a file e.g. routes/abc.js:

and then in more general router e.g. in routes/index.js:

const moreSpecificabc = require('more-specific''abc');
const moreGeneralrouter = express.Router();
moreGeneralrouter.use('/abc', moreSpecificabc);
// and export the main router for other modules like app.js to use:
module.exports = router;

I would recommend exporting a more specific router:

and then in more general router:

const moreSpecific = require('more-specific');
const moreGeneral = express.Router();
moreGeneral.use('/abc', moreSpecific);

I would recommend exporting a more specific router in a file e.g. routes/abc.js:

and then in more general router e.g. in routes/index.js:

const abc = require('abc');
const router = express.Router();
router.use('/abc', abc);
// and export the main router for other modules like app.js to use:
module.exports = router;
added 853 characters in body
Source Link
rsp
  • 111.7k
  • 31
  • 209
  • 183
module exports = (router) => {
  router.use('/x'abc/xyz', ...);
};
const router = express.Router();
router.use('/x'xyz', ...);
module exports = router;
const moreSpecific = require('more-specific');
const moreGeneral = express.Router();
moreGeneral.use('/y'abc', moreSpecific);

to have a route like /yabc/xxyz.

module exports = (router) => {
  router.use('/x', ...);
};
const router = express.Router();
router.use('/x', ...);
module exports = router;
const moreSpecific = require('more-specific');
const moreGeneral = express.Router();
moreGeneral.use('/y', moreSpecific);

to have a route like /y/x

module exports = (router) => {
  router.use('/abc/xyz', ...);
};
const router = express.Router();
router.use('/xyz', ...);
module exports = router;
const moreSpecific = require('more-specific');
const moreGeneral = express.Router();
moreGeneral.use('/abc', moreSpecific);

to have a route like /abc/xyz.

added 853 characters in body
Source Link
rsp
  • 111.7k
  • 31
  • 209
  • 183
Loading
Source Link
rsp
  • 111.7k
  • 31
  • 209
  • 183
Loading