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 8568283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:04:50+00:00 2026-06-11T18:04:50+00:00

I have an express node app, and I’m trying to keep my code neat

  • 0

I have an express node app, and I’m trying to keep my code neat by not having all the socket.io stuff in app.js

I don’t know the best way to go about this. Here is my initial thought which doesn’t feel like the cleanest one

// app.js
var express = require('express')
    , app = express()
    , server = require('http').createServer(app)
    , url = require('url')
    , somePage = require('./routes/somePage.js')
    , path = require('path');

    app.configure(function(){...});

    app.get('/', somePage.index);

and the route

// somePage.js
    exports.index = function (req, res, server) {
        io = require('socket.io').listern(server)
        res.render('index',{title: 'Chat Room'})

        io.sockets.on('connection', function(socket) {
            ...code...
        }
    }

I feel like I’m close but not quite there

  • 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-11T18:04:52+00:00Added an answer on June 11, 2026 at 6:04 pm

    I don’t know if I’m reading that right but it looks like you are starting a socket server on every request for /, which I’m frankly a little surprised works at all.

    This is how I’m separating out the socket.io code from app.js (using express 3.x which is a bit different than 2.x):

    // app.js
    var express = require('express');
    var app = express();
    
    var server_port = config.get('SERVER_PORT');
    server = http.createServer(app).listen(server_port, function () {
        var addr = server.address();
        console.log('Express server listening on http://' + addr.address + ':' + addr.port);
    });
    
    var sockets = require('./sockets');
    sockets.socketServer(app, server);
    
    
    // sockets.js
    var socketio = require('socket.io');
    exports.socketServer = function (app, server) {
      var io = socketio.listen(server);
    
      io.sockets.on('connection', function (socket) {
        ...
      });
    };
    

    Hope that helps!

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

Sidebar

Related Questions

I have the following express node.js app. It's using the 'redis' npm package. app.get(/test,function(req,res){
I have a JavaScript data structure like the following in my Node.js/Express web app:
I have a Node.js Express app that I'm hosting on Nodejitsu. I'm already using
I have a basic Node.js app that I am trying to get off the
I'm using node, express and connect for a simple app and have basic HTTP
I have been trying to figure out how to integrate a node.js app into
In my AngularJS node.js app based on angular-express-blog and express-coffee I have an issue
I am using node.js + express.js + everyauth.js. I have moved all my everyauth
I have the following data strucutre outputting form my Schema in a node/express app.
I have the following error while trying to start my node app: TypeError: Cannot

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.