连接时socket.io没有显示console.log消息

时间:2019-06-21 18:27:16

标签: javascript node.js express socket.io

每当有人连接到我的服务器时,我都在尝试console.log一条消息。请告知我做错了什么或如何改进我的代码。

server.js

// express server setup
const express = require('express')
const bodyParser = require('body-parser')
const cors = require('cors')
const app = express()
const server = require('http').createServer(app)
const io = require('socket.io')(server)
const port = process.env.PORT || 1991

// middleware
app.use(cors())

//api
const metrics = require('./routes/api/metrics')
app.use('/api/metrics', metrics)

// 
server.listen(port, () => {
    console.log(`server running @ port ${port}`);
})

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

0 个答案:

没有答案