每当有人连接到我的服务器时,我都在尝试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')
})