Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Divide Node App in different files

Divide Node App in different files


I'm developing my first Node.js App with Socket.IO and everything is fine but now the app is slowly getting bigger and I'd like to divide the app-code into different files for better maintenance.

For example I'm defining all my mongoose schemas and the routings in the main file. Underneath are all the functions for the socket.IO connection. But now I want to have an extra file for the schemas, an extra file for routing and one for the functions.

Of course, I'm aware of the possibility to write my own module or load a file with require. That just does not make sense for me, because I can't work with the vars like app, io or db without making them global. And if I pass them to a function in my module, I can't change them. What am I missing? I'd like to see an example how this is done in practice without using global vars..

Problem courtesy of: Benjamin


It sounds like you have a highly coupled Application; it's difficult for you to split out your code into modules because pieces of the application that should not depend on each other do. Looking into the principles of OO design may help out here.

For example, if you were to split your dataabse logic out of the main application, you should be able to do so, as the database logic should not depend on app or io--it should be able to work on its own, and you require it into other pieces of your application to use it.

Here's a fairly basic example--it's more pseudocode than actual code, as the point is to demonstrate modularity by example, not to write a working application. It's also only one of many, many ways you may decide to structure your application.

// =============================
// db.js

var mongoose = require('mongoose');
mongoose.connect(/* ... */);

module.exports = {
  User: require('./models/user');
  OtherModel: require('./models/other_model');

// =============================
// models/user.js (similar for 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;

// This module exports a function, which we call call with
// our Express application and Socket.IO server as arguments
// so that we can access them if we need them.
module.exports = function(app, io) {
  app.get('/', function(req, res) {
    // home page logic ...
  });'/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('');
var routes = require('./routes');
var realtime = require('./realtime');

var app = express();
var server = http.createServer(app);
var io = sio.listen(server);

// all your app.use() and app.configure() here...

// Load in the routes by calling the function we
// exported in routes.js
routes(app, io);
// Similarly with our realtime module.


This was all written off the top of my head with minimal checking of the documentation for various APIs, but I hope it plants the seeds of how you might go about extracting modules from your application.

Solution courtesy of: Michelle Tilley


View additional discussion.

This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Divide Node App in different files


Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription