node.js - Divide Node App in different files -


i'm developing first node.js app socket.io , fine app getting bigger , i'd divide app-code different files better maintenance.

for example i'm defining mongoose schemas , routings in main file. underneath functions socket.io connection. want have file schemas, file routing , 1 functions.

of course, i'm aware of possibility write own module or load file require. not make sense me, because can't work vars app, io or db without making them global. , if pass them function in module, can't change them. missing? i'd see example how done in practice without using global vars..

it sounds have highly coupled application; it's difficult split out code modules because pieces of application should not depend on each other do. looking the principles of oo design may out here.

for example, if split dataabse logic out of main application, should able so, database logic should not depend on app or io--it should able work on own, , require other pieces of application use it.

here's basic example--it's more pseudocode actual code, point demonstrate modularity example, not write working application. it's 1 of many, many ways may decide structure application.

// ============================= // db.js  var mongoose = require('mongoose'); mongoose.connect(/* ... */);  module.exports = {   user: require('./models/user');   othermodel: require('./models/other_model'); };   // ============================= // models/user.js (similar models/other_model.js)  var mongoose = require('mongoose'); var user = new mongoose.schema({ /* ... */ }); module.exports = mongoose.model('user', user);   // ============================= // routes.js  var db = require('./db'); var user = db.user; var othermodel = db.othermodel;  // module exports function, call call // our express application , socket.io server arguments // can access them if need them. module.exports = function(app, io) {   app.get('/', function(req, res) {     // home page logic ...   });    app.post('/users/:id', function(req, res) {     user.create(/* ... */);   }); };   // ============================= // realtime.js  var db = require('./db'); var othermodel = db.othermodel;  module.exports = function(io) {   io.sockets.on('connection', function(socket) {     socket.on('someevent', function() {       othermodel.find(/* ... */);     });   }); };   // ============================= // application.js  var express = require('express'); var sio = require('socket.io'); var routes = require('./routes'); var realtime = require('./realtime');  var app = express(); var server = http.createserver(app); var io = sio.listen(server);  // app.use() , app.configure() here...  // load in routes calling function // exported in routes.js routes(app, io); // our realtime module. realtime(io);  server.listen(8080); 

this written off top of head minimal checking of documentation various apis, hope plants seeds of how might go extracting modules application.


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -