-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.js
65 lines (58 loc) · 1.55 KB
/
router.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
/*
* Router is responsible for route different request
*/
function route(pathname, response, request, dbClient) {
console.log('About to route request for ' + pathname);
if (request.method == 'POST') {
console.log(pathname + " is POST request");
dbClient.storeDB(request, response);
} else if (request.method == 'GET') {
console.log(pathname + ' is GET method');
if (pathname == '/') {
showHomePage(response);
return;
} else if (pathname == '/favicon.ico') {
return;
}
dbClient.searchDB(pathname, response);
} else {
showGenericErrorPage();
}
}
function showHomePage(response) {
var body = '<html>'+
'<head>'+
'<meta http-equiv="Content-Type" '+
'content="text/html; charset=UTF-8" />'+
'</head>'+
'<body>'+
'<form action="/upload"'+
'method="post">'+
'<p>Url you want to shorten</p>' +
'<input type="text" name="originalUrl">'+
'<br/>' +
'<p>TinyUrl you want to map</p>' +
'<input type="text" name="tinyUrl">'+
'<input type="submit" value="Submit" />'+
'</form>'+
'</body>'+
'</html>';
response.writeHead(200, {'Content-Type' : 'text/html'});
response.write(body);
response.end();
}
function showGenericErrorPage(response) {
var body = '<html>'+
'<head>'+
'<meta http-equiv="Content-Type" '+
'content="text/html; charset=UTF-8" />'+
'</head>'+
'<body>'+
'<p> Invalid Request </p>'+
'</body>'+
'</html>';
response.writeHead(404, {'Content-Type' : 'text/html'});
response.write(body);
response.end();
}
exports.route = route;