Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8808917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:44:26+00:00 2026-06-14T02:44:26+00:00

I’m developing my first Node.js App with Socket.IO and everything is fine but now

  • 0

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..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T02:44:27+00:00Added an answer on June 14, 2026 at 2:44 am

    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 ...
      });
    
      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);
    
    // 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.
    realtime(io);
    
    server.listen(8080);
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
We're building an app, our first using Rails 3, and we're having to build
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.