Last active
August 27, 2016 20:12
-
-
Save yuricamara/a567b7dab838d54a07101bf281beae52 to your computer and use it in GitHub Desktop.
MVC loadign without express-load
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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