Skip to content

Instantly share code, notes, and snippets.

@konsumer
Last active December 12, 2015 02:24
Show Gist options
  • Save konsumer/5e9723b4d3daa29beeae to your computer and use it in GitHub Desktop.
Save konsumer/5e9723b4d3daa29beeae to your computer and use it in GitHub Desktop.
Require all the files in a directory as members of `module.exports`
/*
Generic include that loads all files as members with their own filename, good for `models/` for example
*/
var fs = require('fs')
fs.readdirSync(__dirname)
.filter(function(n){
return n[0]!=='.' && n !=='index.js' && n.substr(-3) === '.js'
})
.map(function(n){
var name = n.split('.')[0]
module.exports[name] = require('./' + n)
})
var mongoose = require('mongoose')
var User = new mongoose.Schema({
name: String
})
module.exports = mongoose.model('User', User)
module.exports = require('express').Router()
module.exports.get('/users/:user', function (req, res) {
res.json(req.user)
})
var express = require('express')
var mongoose = require('mongoose')
var app = express()
var models = require('./models/') // put index.js & your model files in dir called `models`
var routes = require('./routes/') // put index.js & your route files in dir called `routes`
// http://mongoosejs.com/docs/connections.html
mongoose.connect('mongodb://localhost/myapp')
// you can also get this same model anywhere with mongoose.model('User')
// this comes from a file called models/User.js
models.User.find({name: 'David Konsumer'}, function(err, users){
if (err) throw err
console.log(users)
})
// manual-mapping to URL endpoints, this could be done automatically in routes/index.js in the require loop (using `/${name}`)
// this comes from a file called routes/api.js
app.use('/api', routes.api)
@knotech
Copy link

knotech commented Dec 8, 2015

Okay, I see it now.

So I don't have to declare each route||controller||whatever individually in the app.js like:

var storeController = require('./controllers/store.js');
var aboutController = require('./controllers/about.js');
var blogController = require('./controllers/blog.js');

etc...

And with this setup I don't have to declare each model in each route either like

var User = require('../models/User');

exports.postSignup = function(req, res, next) {
  req.sanitize('attribute');
  req.assert('attribute');
  var user = new User({
     userAtrribute : req.attribute
  });

// error checks -> save user || do whatever

Since the model is globally available in app.js I already have that and can just grab it cause it's in the namespace.
and I'll be declaring the route as part of the module imported by

var routes = require('./routes/');

in app.js so I'll want to use module.exports, instead of exporting the route with

exports.getRoute = function(req,res) { //stuff }

I want to use:

module.exports.postSignup = function(req, res, next) {
  req.sanitize('attribute');
  req.assert('attribute');
  var user = new models.User({
     userAtrribute : req.attribute
  });

// save user or whatever

Do I have that last part right about module.exports vs. exports?

@knotech
Copy link

knotech commented Dec 8, 2015

Yeah, that's way cleaner.

@knotech
Copy link

knotech commented Dec 12, 2015

Thanks man. This is such a better setup.

I'm in the process of refactoring the whole layout of the site I'm working on. it's tedious, because it's already pretty built up, but for the store section's calls to the product api, and user management, this api routing model makes it so much more manageable as this thing sprawls way out scope.

Appreciate the tutorial man.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment