Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

343
Views
my command repeat with every refresh page in node js

Im using node js with express framework and socket.io in my project.

when i load test page in browser(with this address: http://localhost:3001/test) every thing is fine and print "a user connected" in server log but when i refresh page again print that message two time and with another refresh print that message three times and with every refresh repeat that message.

whats wrong in my code ?

thanks.

test.js

var express = require('express');
var router = express.Router();
var io = require('../app');

router.get('/', function(req, res, next) {

    io.on('connection', function(socket){
        console.log('a user connected');
    });

    res.render('test', { title: 'test' });
});

module.exports = router;

app.js

    var express = require('express');
    var path = require('path');
    var favicon = require('serve-favicon');
    var logger = require('morgan');
    var session = require('express-session')
    var cookieParser = require('cookie-parser');
    var bodyParser = require('body-parser');

    var app = express();
    var server = require('http').Server(app);
    var io = require('socket.io')(server);

    server.listen(3001);

    module.exports = io;

    var index = require('./routes/index');
    var users = require('./routes/users');
    var test = require('./routes/test');

    // view engine setup
    app.set('views', path.join(__dirname, 'views'));
    app.set('view engine', 'hbs');

    // uncomment after placing your favicon in /public
    //app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
    app.use(logger('dev'));
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({ extended: false }));
    app.use(cookieParser());
    app.use(express.static(path.join(__dirname, 'public')));

    app.use('/', index);
    app.use('/users', users);
    app.use('/test', test);

    // catch 404 and forward to error handler
    app.use(function(req, res, next) {
      var err = new Error('Not Found');
      err.status = 404;
      next(err);
    });

    // error handler
    app.use(function(err, req, res, next) {
      // set locals, only providing error in development
      res.locals.message = err.message;
      res.locals.error = req.app.get('env') === 'development' ? err : {};

      // render the error page
      res.status(err.status || 500);
      res.render('error');
    });

    module.exports = app;
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You are adding an additional event handler on every GET / request.

You only need to add the event handler once, outside of a http route. Think of it as the socketio equivalent of a route.

var io = require('socket.io')(server);

io.on('connection', function(socket){
  console.log('a user connected');
});

Then in your router, just deal with the http request

router.get('/', function(req, res, next) {
  res.render('test', { title: 'test' });
});
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!