-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
49 lines (39 loc) · 1.3 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
var express = require("express");
const app = express()
var restRouter = require("./routes/rest");
var indexRouter = require("./routes/index");
var path = require('path');
var mongoose = require("mongoose");
// for socket, can not use express's app.listen, because it's used by main service
var http = require('http');
var socket_io = require('socket.io');
var io = socket_io();
var socketService = require("./services/socketService.js")(io);
// mongodb://<dbuser>:<dbpassword>@ds213259.mlab.com:13259/oj
// <dbuser> == user, <dbpassword> == user
mongoose.connect("mongodb://user:[email protected]:13259/oj");
app.use(express.static(path.join(__dirname, '../public')));
app.use('/', indexRouter)
app.use("/api/v1", restRouter);
// handle refresh and direct webpage address other than '/'
app.use(function(req, res) {
// send index.html to start client side
res.sendFile("index.html", {
root: path.join(__dirname, '../public/')
});
});
var server = http.createServer(app);
io.attach(server);
server.listen(3000);
server.on('error', onError);
server.on('listening', onListening);
function onError(error) {
throw error;
}
function onListening() {
var addr = server.address();
var bind = typeof addr == 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
console.log('Listening on ' + bind);
}