socket.io出现问题,当我启动Express应用程序时,它返回此错误GET /socket.io/?EIO=3&transport=polling&t=1557980690629-0 404 0.758 ms - 1413
这是我的app.js
let createError = require('http-errors');
let express = require('express');
let path = require('path');
let cookieParser = require('cookie-parser');
let logger = require('morgan');
let bodyparser = require('body-parser');
let mongoose = require('mongoose');
let session = require('express-session')
let app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
io.on('connection', () =>{
console.log('connected')
})
app.use(session({
secret:'asd123',
resave: false,
saveUninitialized: true,
cookie:{
seccure: true
}
}))
app.use((req,res,next)=>{
app.locals.username = req.session.username || ""
app.locals.userid = req.session.userid || ""
next()
})
app.use((req, res, next) => {
mongoose.connect('mongodb://localhost/inventory', {
useNewUrlParser: true,
keepAlive: true,
keepAliveInitialDelay: 300000
});
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', () => {
req.db = mongoose.connection;
next()
});
})
const checkuser = (req,res,next)=>{
if(req.session.username && req.session.userid && req.session.level){
next()
}else{
res.redirect('/')
}
}
let indexRouter = require('./routes/index');
let usersRouter = require('./routes/users');
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(logger('dev'));
app.use(bodyparser.json());
app.use(bodyparser.urlencoded({extended: true}));
app.use(express.json());
app.use(express.urlencoded({extended: false}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', indexRouter);
app.use('/users', usersRouter);
// catch 404 and forward to error handler
app.use( (req, res, next) => {
next(createError(404));
});
// 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;
console.log('connected')
也未在控制台上显示
然后将其添加到我的ejs文件中
<script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
<script>
var socket = io();
</script>
我正在使用带有ejs模板的express-generator生成此文件,该模板也通过mongoose驱动程序与monggodb集成,但是我认为mongoose或monggodb在socket.io上没有任何问题,因为它们具有不同的端口