-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·50 lines (41 loc) · 1.25 KB
/
server.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
const express = require('express')
const app = express();
const bodyParser = require('body-parser');
var mongoose = require('mongoose');
var cors = require('cors');
var config = require('./config');
app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json());
app.use(cors());
app.options('*', cors());
var port = process.env.PORT || 3001;
app.get('/', (req, res) => {
console.log('GET \'/\' url!!')
res.send('Hello World!');
})
let users = require('./api/users');
app.use('/api/users', users);
const mongodbUri = config.mongodbUri();
// const mongodbUri = 'mongodb://localhost:27017'
mongoose.connect(mongodbUri, {
// useMongoClient: true
// useNewUrlParser: true
});
mongoose.Promise = global.Promise;
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
console.log('Connected to mongod server..');
})
// db.on('disconnect', connect);
server = app.listen(port, function() {
console.log('server is running on port '+port);
});
const io = require('socket.io')(server);
io.on('connection', function(socket) {
console.log(socket.id)
socket.on('SEND_MESSAGE', function(data) {
io.emit('MESSAGE', data)
console.log(data);
});
});