-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
45 lines (40 loc) · 1.35 KB
/
index.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
import http from 'http';
import url from 'url';
import validUrl from 'valid-url';
import { getImage } from './modules/controller.js';
import { logHttpRequest } from './modules/utils.js';
const port = 8000;
const server = http.createServer((req, res) => {
const reqUrlParse = url.parse(req.url, true);
const path = reqUrlParse.pathname;
let urlQuery;
if (validUrl.isWebUri(reqUrlParse.query.url)) {
urlQuery = url.parse(req.url, true).query.url;
}
if (req.method == "GET") {
switch (path) {
case "/getImage":
try {
getImage(urlQuery, res);
} catch (error) {
console.log(error);
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.write(String(error));
res.end();
}
break;
default:
res.writeHead(400, { 'Content-Type': 'text/plain' });
res.write("Wrong request, read the documentation");
res.end();
}
} else {
res.writeHead(400, { 'Content-Type': 'text/plain' });
res.write("Method not supported, read the documentation");
res.end();
}
logHttpRequest(req, res);
});
server.listen(port, () => {
console.log(`Server running at port ${port}!`);
});