我有一个 express.js 应用程序可以解决这个问题:
node app.js
info - socket.io started
它不会再进一步,如果有人能解释为什么会发生这种情况以及如何解决它,将不胜感激。我相信错误出现在我的 app.js 中,它发布在此处:
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);