Express服务器socket.io现在出现404错误

时间:2019-01-07 01:22:50

标签: node.js express socket.io

我看过其他一些类似的问题,但是没有一个能帮助我解决问题。我最近开始对我的一个个人项目进行一些更新,但在发现回购未在线运行后,我不得不重新克隆该回购。运行npm i安装我的软件包后,它将在本地打开,但是控制台开始填充此错误:http://localhost:3000/socket.io/?EIO=3&transport=polling&t=MWbrM4h 404(未找到)

我尝试根据此处的其他响应更改为侦听http或直接在io上侦听,但未做任何更改。这是我的代码。

const express = require('express'),

  massive = require('massive'),
  bodyPar = require('body-parser'),
  CombatCtrl = require('./Controllers/CombatCtrl'),
  AuthCtrl = require('./Controllers/AuthCtrl'),
  EnemyCtrl = require('./Controllers/EnemyCtrl'),
  path = require('path'),
  socket_io = require('socket.io'),
  sharedsession = require("express-socket.io-session");
require('dotenv').config();
const session = require('express-session')({
  secret: process.env.SESSION_SECRET,
  resave: false,
  saveUninitialized: true
});

const app = express()

app.use(bodyPar.json())

app.use(session)

app.use(express.static(`${__dirname}/../build`));

app.get('*', (req, res) => {
  res.sendFile(path.join(__dirname, '../build/index.html'));
});

const {
  SERVER_PORT
} = process.env

const server = app.listen(SERVER_PORT, () => {
  console.log('How do you want to do this?', SERVER_PORT)
})

const io = socket_io(server)

io.use(sharedsession(session, {
  autoSave: true
}));

io.on('connection', function(socket) {
      socket.emit('start', /*emit params sent as obj*/ )

 //I have stuff in here, but it 's irrelevant to the landing page 
    }

1 个答案:

答案 0 :(得分:0)

尝试做这样的事情。

const {
  SERVER_PORT
} = process.env

const server = app.listen(SERVER_PORT, () => {
  console.log('How do you want to do this?', SERVER_PORT)
})

const io = socket_io.listen(server)

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