forked from shubham1172/sudocode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
215 lines (174 loc) · 5.41 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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/**
This file is the main server file
It routes the urls to their respective managers.
*/
var express = require('express');
var morgan = require('morgan');
var path = require('path');
var pgp = require('pg-promise')();
var bodyParser = require('body-parser');
var session = require('express-session');
var sanitizer = require('sanitize-html');
var user = require('./user.js');
var feed = require("./feed.js");
var voter = require("./voter.js");
var search = require("./search.js");
var sessionManager = require('./session-manager.js');
var articleManager = require('./article-manager.js');
var commentManager = require('./comment-manager.js');
var categoryManager = require('./category-manager.js');
var config = {
user: 'postgres',
database: 'postgres',
host: '127.0.0.1',
port: '5432',
password: process.env.DB_PASSWORD
};
var pool = pgp(config);
var app = express();
app.use(morgan('combined'));
app.use(bodyParser.json({limit: '5mb'}));
app.use(session({
secret: 'someRandomSecretValue',
cookie: {maxAge: 1000*60*60}
}));
app.get('/', function(req,res){
res.sendFile(path.join(__dirname, 'ui', 'index.html'));
});
//html files
app.get('/:fileName.:html', function(req, res){
res.sendFile(path.join(__dirname, 'ui', req.params.fileName+'.html'));
});
//front end files
app.get('/ui/:fileName', function(req, res){
res.sendFile(path.join(__dirname, 'ui', req.params.fileName));
});
//use case: www.host:port/ui/?filePath=fontawesome/test/sample.txt
app.get('/ui', function (req, res) {
res.sendFile(path.join(__dirname, 'ui', req.query.filePath));
});
//jquery
app.get('/jquery/jquery-2.2.0.min.js', function(req,res){
res.sendFile(path.join(__dirname,'jquery','jquery-2.2.0.min.js'));
});
app.get('/jquery-ui/jquery-ui.min.js', function(req, res){
res.sendFile(path.join(__dirname,'jquery-ui','jquery-ui.min.js'));
});
//hashing
app.get('/hash/:input', function(req, res) {
sessionManager.getHash(req, res);
});
//session management
app.get('/check-login', function(req, res){
sessionManager.checkLogin(req,res,pool);
});
app.post('/login', function(req, res){
sessionManager.login(req,res,pool);
});
app.get('/logout', function(req, res){
sessionManager.logout(req,res,pool);
});
/** /checkId/?id=xxx */
app.get('/checkId', function(req, res){
sessionManager.checkId(req, res, pool);
});
//article management
app.post('/create-article', function(req, res){
articleManager.createArticle(req,res,pool);
});
/** /delete-article/?id=aid*/
app.get('/delete-article', function(req, res){
articleManager.deleteArticle(req, res, pool);
});
/** /get-articles/?category=Python or /get-articles/?userId=123 */
app.get('/get-articles', function(req, res){
articleManager.getArticle(req,res,pool);
});
/** /edit-article/*/
app.post('/edit-article', function(req, res){
articleManager.editArticle(req,res,pool);
});
//comment management
app.post('/create-comment', function(req, res){
commentManager.createComment(req,res,pool);
});
/** /delete-comment/?id=cid*/
app.get('/delete-comment', function(req, res){
commentManager.deleteComment(req, res, pool);
});
/** /get-comments/?aid=123*/
app.get('/get-comments', function(req, res){
commentManager.getComment(req,res,pool);
});
app.post('/edit-comment', function(req, res){
commentManager.editComment(req,res,pool);
});
//category management
app.get('/get-categories', function(req, res){
categoryManager.getCategory(req,res,pool);
});
app.get('/get-allowed-tags', function(req, res){
categoryManager.getAllowedTags(req,res,pool);
});
//user actions
app.post('/set-username', function(req, res){
user.setUsername(req,res,pool);
});
app.post('/set-bio', function(req, res){
user.setBio(req,res,pool);
});
app.post('/set-photo', function(req, res){
user.setPhoto(req,res,pool);
});
app.get('/remove-photo', function(req, res){
user.removePhoto(req,res,pool);
});
app.get('/remove-bio', function(req, res){
user.removeBio(req,res,pool);
});
app.get('/get-username', function(req, res){
user.getUsername(req,res,pool);
});
app.get('/get-bio', function(req, res){
user.getBio(req,res,pool);
});
app.get('/get-user', function(req, res){
user.getUser(req,res,pool);
});
//returns self photo by default or ?id=xxx
app.get('/get-photo', function(req, res){
user.getPhoto(req,res,pool);
});
app.post('/change-password', function(req, res){
user.changePassword(req,res,pool);
});
app.post('/deactivate', function(req, res){
user.deactivate(req,res,pool);
});
//can be used as /vote/?aid=xx&value=#
app.get('/vote', function(req, res){
voter.vote(req,res,pool);
});
//can be used as /delete-vote/?aid=xx
app.get('/delete-vote', function(req, res){
voter.deleteVote(req,res,pool);
});
//can be used as /get-vote/?aid=xx
app.get('/get-vote', function(req, res){
voter.getVote(req,res,pool);
});
//can be used as /search/?q=xxxyyy
app.get('/search', function(req, res){
search.searchQuery(req,res,pool);
});
app.get('/get-feed', function(req, res){
feed.getArticles(req,res,pool);
});
//can be used as /check-username/?username=username
app.get('/check-username', function(req, res){
user.checkUserAvailable(req, res, pool);
});
var port = 8082;
app.listen(port, function(){
console.log('SUDOCODE up and running on 8082!');
});