-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
91 lines (80 loc) · 3.06 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
89
90
91
var express = require('express');
var app = express();
require('dotenv').load();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var path = require('path');
var morgan = require('morgan');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
var expressValidator = require('express-validator');
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var config = require('./backend/config/appConfig');
var authConfig = require('./backend/auth/authConfig');
var socketConfig = require('./backend/auth/socketConfig');
var socketApi = require('./backend/api/socketio/client');
var responseMw = require('./backend/config/responseMiddleware');
authConfig(passport);
socketConfig(io);
socketApi(io);
mongoose.connect(config.connectionStrings.local);
//sample korisnik, ovo maknit kasnije
var uq = require('./backend/queries/userQueries');
var jdid;
uq.addNew("Test User", "123", "test", ["Alpinist", "Doktor"], "Split")
uq.addNew("Josip Dujić", "1234", 2, ["Alpinist", "Programer"], "Zadar").then(data => jdid = data._id)
uq.addNew("Ivan Medić", "12345", 2, ["Doktor", "Alpinist"], "Split")
uq.addNew("Diana Šperanda", "123567", 2, ["Programer"], "Zagreb")
app.use(express.static(__dirname + '/frontend'));
app.use("/uploads", express.static(__dirname + '/uploads'));
app.use(morgan('dev'));
app.use(bodyParser.urlencoded({'extended':'true'}));
app.use(bodyParser.json());
app.use(bodyParser.json({ type: 'application/vnd.api+json' }));
app.use(methodOverride());
responseMw(app, io);
http.listen(config.port, function(){
console.log("listening on", config.port)
});
app.use(function(req, res, next) {
res.setHeader("Access-Control-Allow-Methods", "POST, PUT, OPTIONS, DELETE, GET");
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
next();
});
app.use(expressValidator({
errorFormatter: function(param, msg, value) {
var namespace = param.split('.')
, root = namespace.shift()
, formParam = root;
while(namespace.length) {
formParam += '[' + namespace.shift() + ']';
}
return {
param : formParam,
msg : msg,
value : value
};
}
}));
app.use('/api/auth', require('./backend/api/authApi'));
app.use('/api/home', require('./backend/api/home'));
app.use('/api/user', require('./backend/api/user'));
app.use('/api/action', require('./backend/api/action'));
app.use('/api/group', require('./backend/api/group'));
app.use('/', require('./backend/config/routes'));
app.use(function(err, req, res, next) {
console.log("***********");
console.log("OOPS: ", err);
console.log("***********")
res.status(err.status || 500);
if(config.enableLogging){
Raven.captureException(err);
}
res.json({
errors: err
});
});
module.exports = app;