nebula/server/index.js

49 lines
1.2 KiB
JavaScript
Raw Normal View History

2020-06-22 17:13:42 +08:00
const { Nuxt, Builder } = require('nuxt')
2020-08-10 00:44:27 +08:00
const http = require('http')
2020-06-22 17:13:42 +08:00
const app = require('express')()
2020-08-10 00:44:27 +08:00
const socketio = require('socket.io')
2020-06-22 17:13:42 +08:00
const isProd = (process.env.NODE_ENV === 'production')
const port = process.env.PORT || 3000
// 用指定的配置对象实例化 Nuxt.js
const config = require('./nuxt.config.js')
config.dev = !isProd
const nuxt = new Nuxt(config)
2020-08-10 00:44:27 +08:00
const server = new http.Server(app)
const io = socketio(server, { transports: ['websocket'] })
2020-06-22 17:13:42 +08:00
// 用 Nuxt.js 渲染每个路由
app.use(nuxt.render)
app.set("port", port)
// 在开发模式下启用编译构建和热加载
if (config.dev) {
new Builder(nuxt).build()
.then(listen)
} else {
listen()
}
function listen () {
// 服务端监听
app.listen(port, '0.0.0.0')
console.log('Server listening on `localhost:' + port + '`.')
}
2020-08-10 00:44:27 +08:00
// Socket.io
const messages = []
io.on('connection', (socket) => {
socket.on('last-messages', function (fn) {
console.log('messages-server', messages);
fn(messages.slice(-50))
});
socket.on('all', function (message) {
console.log('message-server', message);
messages.push(message)
socket.broadcast.emit('new-message', message)
});
})