--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
var loginHandler = require('./loginHandler');app.get('/login', loginHandler);
module.exports = function(req,res) { /* perform auth */ };
var itemStore = require('./itemStore');app.post('/item/:id', function(req,res) {
var item = req.body;var id = req.params.id;itemStore.saveItem(id, item, function(err, result) {
if (err) return res.send(err,500);res.send(result);
});
});
module.exports.saveItem = function(id, data) { /* parse some data, save an item */ };module.exports.retrieveItem = function(id) { /* retrieve item, format data, return data */ };
var Store = require('genericStore');var itemStore = new Store('items');var userStore = new Store('users');itemStore.init(/* some config or bootstrap data */);userStore.init(/* some config or bootstrap data */);itemStore.find(...);userStore.updateName(...);
There is also putting you express app variable in a module and doing routing in the different modules you load.
var app = require("server.is");
app.get("/foo",function(req,res,next){});
--
var express = require('express');
var app = express();
// ... configure express, etc...module.exports = app;
var app = require('./app');require('./userRoutes');require('./itemRoutes');// ... start your app, etc...
var app = require('./app'); // same instance as in index.jsapp.get('/user/:id', function(req, res) { ... });app.get('/user/:id/friends', function(req, res) { ...});
var app = require('./app'); // same instance as in index.js and userRoutes.jsapp.get('/item/:id', function(req, res) { ... });app.post('/item/:id', function(req, res) { ...});
You have to make sure when exposing app (or any object or value) via exports that it is initialized prior to being required by any other module. So in this case the routing module would break if it were required by server.js prior to app having been initialized. I use this technique a lot. It allows routing modules to act as controllers. Combined with common.js ability to modularize whole folders through index.js then allows encapsulating all your controllers in a structured manner. I wrote an article on this very subject which can be found at http://jefftschwartz.wordpress.com/2012/09/10/taming-those-unruly-routes/. This article was a follow up to another article which explores using folders for encapsulation in common.js and that article can be found at http://jefftschwartz.wordpress.com/2012/08/23/use-express-route-specific-middleware-to-access-mongodb/.
When you say library, I almost start to think of npm and the modules in there, and I most certainly don't think you mean that, or that anyone would want my super-specific application code in NPM.
When I think of a library, I think of an API like jQuery or a plugin like TouchSwipe for touch events. I'm just a bit unsure what you mean by library here
Thanks a bunch
-Keith