Last active
December 12, 2015 02:24
-
-
Save konsumer/5e9723b4d3daa29beeae to your computer and use it in GitHub Desktop.
Require all the files in a directory as members of `module.exports`
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
/* | |
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) | |
}) |
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
var mongoose = require('mongoose') | |
var User = new mongoose.Schema({ | |
name: String | |
}) | |
module.exports = mongoose.model('User', User) |
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
module.exports = require('express').Router() | |
module.exports.get('/users/:user', function (req, res) { | |
res.json(req.user) | |
}) |
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
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) | |
Yeah, that's way cleaner.
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
Okay, I see it now.
So I don't have to declare each route||controller||whatever individually in the app.js like:
etc...
And with this setup I don't have to declare each model in each route either like
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
in app.js so I'll want to use module.exports, instead of exporting the route with
I want to use:
Do I have that last part right about module.exports vs. exports?