Reduce your code with node-loopie. Routes declaration, models, and etc... node-loopie will shorten it for you. Converts your nodejs files in a folder to an object.
- Declarative: node-loopie helps you shorten your code, you don't have to declare multiple properties to export your object or even initialize function(s). It makes recognition of objects easier with just looking at your file names.
Install with npm:
$ npm install --save node-loopieArguments
nl(folder_path, callback, ignore_file_names, extension_name)
folder_path (required): [String] folder path of the files you want to loop.callback: [Function] function to be executed. passes 3 arguments:file[String],filename[String],file[File].ignore_file_names: [Array (String)] array of file names to be ignored in a folder.extension_nameextension name of the files you want to loop from.
constLoopie=require('node-loopie');constpath=require('path');constSequelize=require('sequelize');constenv=NODE_ENV||'localhost';constconfig=require('/../config/config.js')[env];constdb={};letsequelize;if(config.use_env_variable){sequelize=newSequelize(process.env[config.use_env_variable],config);}else{sequelize=newSequelize(config.database,config.username,config.password,config);}// Get all the filename inside the models folderLoopie(__dirname,(file)=>{constmodel=require(path.join(__dirname,file))(sequelize,Sequelize.DataTypes);db[model.name]=model;});Object.keys(db).forEach((modelName)=>{if(db[modelName].associate){db[modelName].associate(db);}});db.sequelize=sequelize;db.Sequelize=Sequelize;module.exports=db;constexpress=require('express');constLoopie=require('node-loopie');constapp=express();Loopie(__dirname,(file,fileName)=>{app.use(`/api/${fileName}`,require(path.join(__dirname,file)));});Axl Cuyugan
Released under the ISC License.