-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
118 lines (93 loc) · 3.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
'use strict';
let express = require('express');
/*
require('dotenv').config({ path: './server/config/' + process.env.NODE_ENV.trim() + '/.env' });
*/
require('dotenv').config({ path: './server/config/local/.env' });
console.log('Server environment : ' + process.env.NODE_ENV.trim());
let app = express(),
cors = require('cors'),
port = process.env.PORT || 3000,
mongoose = require('mongoose'),
jsonwebtoken = require("jsonwebtoken"),
uuidv4 = require('uuid/v4');
// Load models
let User = require('./server/api/models/userModel'),
bodyParser = require('body-parser'),
cookieParser = require('cookie-parser');
// Enable All CORS Requests
app.options('*', cors({credentials: true}));
// Interceptor : verify the authorization
app.use(function(req, res, next) {
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', 'http://localhost:8080');
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, PATCH, DELETE');
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true);
// To fix the CORS OPTIONS
if(req && req.method=="OPTIONS"){
req.method=req.headers["access-control-request-method"];
}
if (req.headers && req.headers.authorization && req.headers.authorization.split(' ')[0] === process.env.AUTHORIZATION_VALUE.trim()) {
jsonwebtoken.verify(req.headers.authorization.split(' ')[1], process.env.SECRET_KEY.trim(), function(err, decode) {
if (err) req.user = undefined;
req.user = decode;
next();
});
} else {
req.user = undefined;
next();
}
});
// mongoose instance connection url connection
// mongoose.Promise = global.Promise;
console.log('try the connection');
if (process.env.NODE_ENV.trim() == 'local') {
console.log('local connection');
mongoose.connect('mongodb://' + process.env.DB_HOST.trim() + '/' + process.env.DB_DATABASE.trim());
} else {
console.log('remote connection');
mongoose.connect('mongodb://' + process.env.DB_USER.trim() + ':' + process.env.DB_PASSWORD.trim() + '@' + process.env.DB_HOST.trim() + '/' + process.env.DB_DATABASE.trim())
}
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser());
//importing routes
let userRoutes = require('./server/api/routes/userRoutes');
userRoutes(app);
// Session
let session = require('express-session'),
MongoStore = require('connect-mongo')(session),
passport = require('passport');
app.use(session({
cookie: { secure: true },
saveUninitialized: false,
resave: false,
secret: "music",
store: new MongoStore({
mongooseConnection: mongoose.connection
}),
resave: true,
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(function(req, res) {
res.status(404).send({ url: req.originalUrl + ' is not implemented' })
});
const server = app.listen(port, () => {
console.log('Guitars : RESTful API server started on: ' + port);
});
// Using WebSockets
const io = require('socket.io')(server);
io.on('connection', function(socket) {
socket.on('SEND_MESSAGE', function(data) {
io.emit('MESSAGE', data)
});
socket.on("error", function(err) {
socket.emit("GOT_ERROR", err);
});
});