-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
157 lines (129 loc) · 4.24 KB
/
app.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
* Module dependencies
*/
var express = require('express'),
handlebars = require('express3-handlebars'),
bodyParser = require('body-parser'),
methodOverride = require('method-override'),
// errorHandler = require('error-handler'),
morgan = require('morgan'),
jwt = require('jwt-simple'),
_ = require('underscore'),
routes = require('./routes'),
api = require('./routes/api'),
http = require('http'),
path = require('path');
var app = module.exports = express();
//mongoDB setup
var mongoose = require('mongoose');
mongoose.connect(process.env.MONGOLAB_URI || 'mongodb://localhost/calasia');
//load environment variables
var dotenv = require('dotenv');
dotenv.load();
/**
* Configuration
*/
// all environments
app.set('port', process.env.PORT || 3000);
app.set('jwtTokenSecret','123456ABCDEF');
app.engine('handlebars', handlebars());
app.set('view engine', 'handlebars');
app.set('views', __dirname + '/views');
// app.set('view engine', 'jade');
app.use(morgan('dev'));
app.use(bodyParser.json());
app.use(methodOverride());
app.use(express.static(path.join(__dirname, 'public')));
var tokens = [];
function requiresAuthentication(request, response, next) {
// console.log(request.headers);
if (request.headers.access_token) {
var token = request.headers.access_token;
if (_.where(tokens, token).length > 0) {
var decodedToken = jwt.decode(token, app.get('jwtTokenSecret'));
if (new Date(decodedToken.expires) > new Date()) {
next();
return;
} else {
removeFromTokens();
response.end(401, "Your session is expired");
}
}
}
response.end(401, "No access token found in the request");
}
function removeFromTokens(token) {
for (var counter = 0; counter < tokens.length; counter++) {
if (tokens[counter] === token) {
tokens.splice(counter, 1);
break;
}
}
}
var env = process.env.NODE_ENV || 'development';
// development only
// if (env === 'development') {
// app.use(express.errorHandler());
// }
// production only
if (env === 'production') {
// TODO
}
/**
* Routes
*/
// serve index and view partials
app.get('/', routes.index);
app.get('/partials/:name', routes.partials);
// JSON API
app.get('/api/events', api.events);
app.get('/api/event/:id', api.oneEvent);
app.get('/api/upcomingEvents', api.upcomingEvents);
app.get('/api/pastEvents', api.pastEvents);
app.get('/api/internalEvents', api.internalEvents);
app.get('/api/externalEvents', api.externalEvents);
app.get('/api/upcomingInternalEvents', api.upcomingInternalEvents);
app.get('/api/upcomingExternalEvents', api.upcomingExternalEvents);
app.get('/api/events/:year', api.yearEvents);
app.post('/api/events/new', api.addEvent);
app.put('/api/event/:id', api.editEvent);
app.delete('/api/events/:id', api.deleteEvent);
app.get('/api/updates', api.updates);
app.get('/api/update/:id', api.oneUpdate)
app.post('/api/updates/new', api.addUpdate);
app.put('/api/update/:id', api.editUpdate);
app.delete('/api/updates/:id', api.deleteUpdate);
app.get('/api/blogs', api.blogs);
app.get('/api/blog/:id', api.oneBlog)
app.post('/api/blogs/new', api.addBlog);
app.put('/api/blog/:id', api.editBlog);
app.delete('/api/blogs/:id', api.deleteBlog);
app.post('/api/login', function (req, res) {
var userName = req.body.userName;
var password = req.body.password;
if (userName === process.env.admin_username && password === process.env.admin_code) {
var expires = new Date();
expires.setDate((new Date()).getDate() + 5);
var token = jwt.encode({
userName: userName,
expires: expires
}, app.get('jwtTokenSecret'));
tokens.push(token);
res.send(200, { access_token: token, userName: userName });
} else {
res.send(401, "Invalid credentials");
}
});
app.post('/api/logout', requiresAuthentication, function (req,res){
var token= req.headers.access_token;
removeFromTokens(token);
res.send(200);
})
// redirect all others to the index (HTML5 history)
app.get('*', routes.index);
/**
* Start Server
*/
http.createServer(app).listen(app.get('port'), function () {
console.log('Express server listening on port ' + app.get('port'));
});