-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
115 lines (89 loc) · 3.55 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
// =================================================================
// get the packages we need ========================================
// =================================================================
var express = require('express');
var app = express();
var cors = require('cors');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var morgan = require('morgan');
var config = require('./config'); // get our config file
// =================================================================
// configuration ===================================================
// =================================================================
var port = process.env.PORT || 8080; // port where app will be runing
mongoose.connect(config.database); // connect to database
mongoose.set('debug', true);
app.set('superSecret', config.secret); // secret variable
// use body parser so we can get info from POST and/or URL parameters
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// seting up app to have cors
app.options('*', cors());
app.all('/*', function(req, res, next) {
// CORS headers
res.header("Access-Control-Allow-Origin", "*");
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header("Access-Control-Allow-Headers", "X-Requested-With, Content-Type");
if (req.method == 'OPTIONS') {
res.status(200);
res.write("Allow: GET,PUT,POST,DELETE,OPTIONS");
res.end();
} else {
next();
}
});
// use morgan to log requests to the console
app.use(morgan('dev'));
var http = require('http').Server(app);
var io = require('socket.io')(http);
// =================================================================
// routes ==========================================================
// =================================================================
// basic route (http://localhost:8080)
app.get('/', function(req, res) {
res.send('Hello! The API is at http://localhost:' + port + '/api');
});
// TODO: move this to routes/default.js
// registering docs view
app.get('/api/docs',function(req,res){
//console.log(__dirname);
res.sendFile(__dirname+'/app/static/templates/docs.html');
});
app.get('/api/sounds/:sound', function(req, res) {
res.download(__dirname+'/app/static/sounds/'+req.params.sound);
});
app.get('/api/test/chat/:room',function(req,res){
//console.log(__dirname);
res.sendFile(__dirname+'/app/static/templates/chat.html');
});
// ---------------------------------------------------------
// get an instance of the router for api routes
// ---------------------------------------------------------
var apiRoutes = express.Router(),
wordRoutes = express.Router(),
tweetRoutes = express.Router(),
chatRoutes = express.Router();
// loading default routes
require('./app/routes/default.js')(app,apiRoutes);
// loading word routes
require('./app/routes/word.js')(app,wordRoutes);
// loading tweet routes
require('./app/routes/tweet.js')(app,tweetRoutes);
// loading chat router
require('./app/routes/chat.js')(app,chatRoutes,io);
app.use('/api', apiRoutes);
app.use('/api', wordRoutes);
app.use('/api', tweetRoutes);
app.use('/api', chatRoutes);
// Making prettier unauthorized method
app.use(function(err, req, res, next){
if (err.constructor.name === 'UnauthorizedError') {
//res.status(401).send('Unauthorized :(');
res.json(err);
}
});
// Listening on port
http.listen(port, function () {
console.log('listening on http://localhost:'+port);
});