-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicRouter.js
More file actions
61 lines (56 loc) · 1.43 KB
/
BasicRouter.js
File metadata and controls
61 lines (56 loc) · 1.43 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
'use strict';
const http = require('http');
const url = require('url');
const qs = require('querystring');
let routes = {
'GET': {
'/': (req, res) => {
res.writeHead(200, {'Content-type': 'text/html'});
res.end('<h1>Hello Router</h1>');
},
'/about': (req, res) => {
res.writeHead(200, {'Content-type': 'text/html'});
res.end('<h1>This is about page</h1>');
},
'/api/getinfo': (req, res) => {
res.writeHead(200, {'Content-type': 'application/json'});
res.end(JSON.stringify(req.queryParams));
}
},
'POST': {
'/api/login': (req, res) => {
let body = '';
req.on('data', data => {
body += data;
if(body.length > 2097152){
res.writeHead(413, {'Content-type': 'text/html'});
res.end('<h3>Error: The file been uploaded exceeds the 2MB limit!');
res.connection.destroy();
}
});
req.on('end', () => {
let params = qs.parse(body);
console.log('Username:', params['username']);
console.log('Password:', params['password']);
res.end();
});
}
},
'NA': (req, res) => {
res.writeHead(404);
res.end('Content not found!')
}
}
function router(req,res){
let baseURI = url.parse(req.url,true);
let resoveRoute = routes[req.method][baseURI.pathname];
if(resoveRoute != undefined){
req.queryParams = baseURI.query;
resoveRoute(req,res);
} else {
routes['NA'](req,res);
}
}
http.createServer(router).listen(3000, () => {
console.log('Surver running on port 3000');
});