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 hidden or 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 hidden or 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 hidden or 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 hidden or 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) | |
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
Yeah, that's way cleaner.