Question

I have an express.js app that get to this:

node app.js
info  - socket.io started

And it won't get any further, if anyone can explain why this happens and how to fix it, it would be greatly appreciated. I believe the error is in my app.js which is posted here:

var express = require('express');
var app = express();
var mongojs = require('mongojs');
var http = require('http');
var db = mongojs('127.0.0.1:27017/mySpendingDB', ['users']);
var server = http.createServer(app);
var io = require('socket.io').listen(server);
var path = require('path');
app.configure(function () {
  app.set('views', __dirname + '/views');
  app.set('view engine', 'ejs');
  app.engine('html', require('ejs').renderFile);
  app.use(express.static(path.join(__dirname, 'public')));
});
app.get('/', function (req, res) {
  res.render('login.html', {
    title: 'Login'
  });
});
app.get('/index', function (req, res) {
  res.render('index.html', {
    title: 'Daily'
  });
});
app.get('/monthly', function (req, res) {
  res.render('monthly.html', {
    title: 'Monthly'
  });
});
app.get('/yearly', function (req, res) {
  res.render('yearly.html', {
    title: 'Login'
  });
});
app.get('/logout', function (req, res) {
  req.logout();
  res.redirect('/');
});
var user = "";
io.sockets.on('connection', function (socket) {
  console.log('socket.io started');
  socket.on('login', function (data) {
    var checkUser = db.users.find(data);
    if (checkUser !== null) {
      user = checkUser.username;
      app.get('/index', function (req, res) {
        res.direct('/index');
      });
      socket.emit('uploadList', checkUser.total);
    }
  });
  socket.on('purchaseLog', function (data) {
    var usercollection = db.users.find({
      username: user
    });
    usercollection.purchase.save(data);
  });
  socket.on('depositLog', function (data) {
    var usercollection = db.users.find({
      username: user
    });
    usercollection.deposit.save(data);
  });
  socket.on('goLogout', function (data) {
    app.get('/logout', function (req, res) {
      req.logout();
      res.redirect('/');
    });
  });
  socket.on('goIndex', function (data) {
    app.get('/index', function (req, res) {
      res.redirect('/index');
    });
  });
  socket.on('goMonthly', function (data) {
    app.get('/monthly', function (req, res) {
      res.redirect('/monthly');
    });
    var monTot = db.users.find({
      username: user
    }).monthlyTotal;
    socket.emit('wentMonthly', monTot);
  });
  socket.on('goYearly', function (data) {
    app.get('/index', function (req, res) {
      res.redirect('/index');
    });
    var yearTot = db.users.find({
      username: user
    }).yearlyTotal;
    socket.emit('wentYearly', yearTot);
  });
  socket.on('registered', function (data) {
    db.users.save(data);
    app.get('/index', function (req, res) {
      res.redirect('/index');
    });
  });
});
app.listen(3000);
Était-ce utile?

La solution

You should be using server.listen(port) instead of app.listen(port). Using app.listen() will only start your Express application, while you also want the HTTP server that socket.io is piggybacking on to start.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top