-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
257 lines (170 loc) · 7.41 KB
/
Copy pathapp.js
File metadata and controls
257 lines (170 loc) · 7.41 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
@Author : Tejas07PSK (Palash Sarkar),
@CreatedON : 02 Dec, 2018, 12:46 PM,
@File-Name : app.js
*/
"use strict";
const createError = require('http-errors');
const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
const ro = require('./resobj');
const indexRouter = require('./routes/index');
const apiRouter = require('./routes/api');
const redirect = require('./routes/redirect');
const app = express();
// set-up view engines
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ 'extended' : true }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
function trimUrlSlash(){ return (((arguments[0]).charAt(arguments[0].length - 1) === '/') ? arguments[0].slice(0, -1) : arguments[0]); }
app.use('/api', function (req, res, next) {
res.set({
'content-type' : "application/json",
'charset' : "utf-8"
});
next();
});
app.use('/api', function (req, res, next) {
if (req.method === 'GET') {
let url_id = req.query['url_id'];
if ((url_id === undefined) || (url_id === null) || (url_id === '')) {
console.log("No \'url_id\' request parameter !!");
res.status(406);
res.end(
JSON.stringify(new ro.ResObj("0", "No \'url_id\ given'. No URL retrieved !! (http - 406)", "")),
"utf-8", function () { console.log("Http conversation ended successfully !!"); }
);
return;
}
if (!((/^[a-zA-Z0-9@*]{6}$/g).test(url_id))) {
console.log("Invalid \'url_id\' request parameter !!");
res.status(406);
res.end(
JSON.stringify(new ro.ResObj("0", "Invalid \'url_id\ request parameter passed !! (http - 406)", "")),
"utf-8", function () { console.log("Http conversation ended successfully !!"); }
);
return;
}
return next();
}
else if (req.method === 'POST') {
let contype = req.headers['content-type'], url = req.body['url'];
if ((contype === undefined) || (contype === null) || (contype !== 'application/json')) {
console.log("Invalid request content type !!");
res.status(400);
res.end(
JSON.stringify(new ro.ResObj("0", "Invalid request content type. Bad request error !! (http - 400)", "")),
"utf-8", function () { console.log("Http conversation ended successfully !!"); }
);
return;
}
if ((url === undefined) || (url === null) || (url === '')) {
console.log("No \'url\' request parameter !!");
res.status(406);
res.end(
JSON.stringify(new ro.ResObj("0", "No \'url\' given. Nothing to shorten !! (http - 406)", "")),
"utf-8", function () { console.log("Http conversation ended successfully !!"); }
);
return;
}
if (!((/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)((?:\/[\+~%\/\.\w\-_]*)?\??(?:[\-\+=&;%@\.\w_]*)#?(?:[\.\!\/\\\w]*))?)/ig).test(url))) {
console.log("Mal-formed \'url\' request parameter !!");
res.status(406);
res.end(
JSON.stringify(new ro.ResObj("0", "The \'url\' passed as a request parameter is mal-formed !! (http - 406)", "")),
"utf-8", function () { console.log("Http conversation ended successfully !!"); }
);
return;
}
req.body['url'] = trimUrlSlash(req.body['url']);
return next();
}
else {
console.log("Http request method not allowed for the access \'URL\' !!");
res.status(405);
res.end(
JSON.stringify(new ro.ResObj("0", "Http request method not allowed for the access 'URL' !! (http - 405)", "")),
"utf-8", function () { console.log("Http conversation ended successfully !!"); }
);
}
});
app.use('/api', apiRouter);
app.use('/:urlid', function (req, res, next) {
res.set({
'content-type' : "text/html",
'charset' : "utf-8"
});
next();
});
app.use('/:urlid', function (req, res, next) {
if (req.method === 'GET') {
let id = req.params['urlid'];
if ((id === undefined) || (id === null) || !((/^[a-zA-Z0-9@*]{6}$/g).test(id))) { console.log("Either no \'urlid\' param has been passed or the value of the \'urlid\' param might be invalid !!"); res.status(404); return next(createError(404)); }
return next();
}
else {
console.log("Http request method not allowed for the access \'URL\' !!");
res.status(405);
return next(createError(405));
}
});
app.use('/:urlid', redirect);
app.use('/', function (req, res, next) {
if (req.method === 'POST') {
res.set({
'content-type' : "text/plain",
'charset' : "utf-8"
});
}
else {
res.set({
'content-type' : "text/html",
'charset' : "utf-8"
});
}
next();
});
app.use('/', function (req, res, next) {
if (req.method === 'POST') {
let contype = req.headers['content-type'], url = req.body['url'];
if ((contype === undefined) || (contype === null) || (contype !== 'application/x-www-form-urlencoded')) {
console.log("Invalid request content type !!");
res.status(400);
res.end("Invalid request content type. Bad request !! (http - 400)", "utf-8", function () { console.log("Http conversation ended successfully !!"); });
return;
}
if ((url === undefined) || (url === null) || (url === '') || !((/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)((?:\/[\+~%\/\.\w\-_]*)?\??(?:[\-\+=&;%@\.\w_]*)#?(?:[\.\!\/\\\w]*))?)/ig).test(url))) {
console.log("Either no \'url\' request parameter has been passed or the value of the \'url\' parameter might be invalid !!");
res.status(406);
res.end("Either no 'url' request parameter has been passed or the value of the 'url' parameter might be invalid. Not acceptable !! (http - 406)", "utf-8", function () { console.log("Http conversation ended successfully !!"); });
return;
}
req.body['url'] = trimUrlSlash(req.body['url']);
return next();
}
else if (req.method === 'GET') { return next(); }
else {
console.log("Http request method not allowed for the access \'URL\' !!");
res.status(405);
res.end("Http request method not allowed for the access \'URL\'. Method not allowed !! (http - 405)", "utf-8", function () { console.log("Http conversation ended successfully !!"); });
}
});
app.use('/', indexRouter);
// catch 404 and forward to error handler
app.use(function (req, res, next) { next(createError(404)); });
// error handler
app.use(function (err, req, res) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = ((req.app.get('env') === 'development') ? err : {});
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;