-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
58 lines (44 loc) · 1.58 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const express= require('express')
const db = require('./mongodb/db')
const config = require('./config')
const route = require('./routes')
const bodyParser = require('body-parser') //请求体数据转成JSON
const history = require('connect-history-api-fallback')
const app = express()
app.use(bodyParser.urlencoded({extended:false}))
app.use(bodyParser.json())
app.use(history());
app.use(express.static('../dist'))
//允许跨域
app.all('*', (req, res, next) => {
res.header('Access-Control-Allow-Origin', req.headers.origin || '*');
res.header('Access-Control-Allow-Headers', 'X-Token, Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild');
res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');
res.header('Access-Control-Allow-Credentials','true'); // 新增
if (req.method == 'OPTIONS') {
res.send(200); /*让options请求快速返回*/
}
else {
next();
}
})
route(app)
const server = require('http').createServer(app);
const io = require('socket.io')(server);
global.io = io;
io.on('connection', function (socket) {
// setTimeout(()=>{
// socket.emit('nodeEvent', { hello: 'world' });
// }, 5000)
socket.on('login_success', (data) => {
//使用user_id作为房间号
socket.join(data.user_id);
console.log('login_success',data);
});
});
io.on('disconnect', function (socket) {
socket.emit('user disconnected');
});
server.listen(config.port, () => {
console.log(`The server is running at http://localhost:${config.port}`);
});