-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
88 lines (66 loc) · 2.51 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
const express = require('express');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
// const FRONTEND_URL = 'http://localhost:3000';
const FRONTEND_URL = 'https://bangor-ave-web.herokuapp.com';
const models = require('./models/user/user.schema.server');
const User = models.getModel('user');
const Chat = models.getModel('chat');
const mongoose = require('mongoose');
// connect to mongo using set team6
let DB_URL = 'mongodb://127.0.0.1:27017/team6'; // for local
if(process.env.MLAB_USERNAME_WEBDEV) { // check if running remotely
DB_URL = process.env.MONGODB_URI;
}
mongoose.connect(DB_URL);
mongoose.connection.on('connected', function(){
console.log('mongo connect success');
});// tell us if the connection is successful.
const userRouter = require('./services/user.service.server');
// create app
const app = express();
app.use(bodyParser.json()); // to parse the json from post
app.use(bodyParser.urlencoded({extended: true}));
//work with express
const server = require('http').Server(app);
const io = require('socket.io')(server);
io.on('connection', function (socket) {
socket.on('sendmsg', function (data) {
const {from, to, msg} = data;
const chatid = [from, to].sort().join('_');
Chat.create(
{chatid, from, to, content: msg},
function (err, doc) {
io.emit('recvmsg', Object.assign({}, doc._doc))
})
// console.log(data);
// io.emit('receiveMessage', data);
})
});
app.use(cookieParser());
app.use(function(req, res, next){
res.header("Access-Control-Allow-Origin",FRONTEND_URL); // we accept response from 4200
res.header("Access-Control-Allow-Headers","Origin, X-Requested-With, Content-Type, Accept");
res.header("Access-Control-Allow-Methods","GET, POST, PUT, DELETE, OPTIONS"); // accept these operations.
res.header("Access-Control-Allow-Credentials","true");
next(); // if successful, pass it through
});
// start a middleware
app.use('/user',userRouter);
app.get('/', function (req, res) {
res.send('home');
});
//friendship
require('./services/friendship.service.server')(app);
//job
//job router
require('./services/job.service.server')(app);
//company router
require('./services/company.service.server')(app);
//application
var applicationService = require('./services/application.service.server');
applicationService(app);
var port = process.env.PORT || 9093;
server.listen(port,function(){
console.log('Node app start at port ' + port);
});