Skip to content

Instantly share code, notes, and snippets.

@yuricamara
Last active August 27, 2016 20:12
Show Gist options
  • Save yuricamara/a567b7dab838d54a07101bf281beae52 to your computer and use it in GitHub Desktop.
Save yuricamara/a567b7dab838d54a07101bf281beae52 to your computer and use it in GitHub Desktop.
MVC loadign without express-load
//server.js
require('../routes')(api);
//routes/index.js
//Carregamento das rotas
const filesNamesFromDirectory = require('../helpers/filesNamesHlp').fromDirectory;
const dir = require('path').join(__dirname, 'routes');
const filesNames = filesNamesFromDirectory(dir);
let route;
filesNames.forEach( fileName => {
route = fileName.replace('Route.js', '');
api.use(`/${route}`, require(`./routes/${fileName}`));
});
//Other files
//require(...)
//Helper filesNamesHlp.js
const fsPkg = require('fs');
exports.fromDirectory = dir => {
const files = [];
fsPkg.readdirSync(dir).forEach( file => {
files.push(file);
});
return files;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment