-
Notifications
You must be signed in to change notification settings - Fork 0
/
framework.js
70 lines (63 loc) · 2.27 KB
/
framework.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
const http = require("http")
let routes = {}
function newRoute(method, route, _callback) {
method = method.toLowerCase()
if(routes[route]) {
routes[route][method] = JSON.stringify(_callback)
} else {
routes[route] = {
[method]: JSON.stringify(_callback)
}
}
routes[route][method] = _callback
}
function listen(port, _callback) {
const server = http.createServer()
function notAvailable(method, route, res) {
res.writeHead(404, {"Content-Type": "text/html"})
res.write(`<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>404 not available</title>
</head>
<body style="overflow: hidden; background-color: black; color: white;">
<div style="display: flex; align-items: center; justify-content: center; height: 100dvh; font-size: 2.5rem; font-family: monospace, system-ui;">can not ${method} "${route}"</div>
</body>
</html>`)
res.end()
}
server.on("request", (req, res) => {
const route = req.url
const method = req.method.toLowerCase()
if(routes[route]) {
if(routes[route][method]) {
res["json"] = function(json) {
res.writeHead(200, {"Content-Type": "application/json"})
res.write(JSON.stringify(json))
}
res["html"] = function(html) {
res.writeHead(200, {"Content-Type": "text/html"})
res.write(html)
}
res["text"] = function(text) {
res.writeHead(200, {"Content-Type": "text/plain"})
res.write(text)
}
res["send"] = function() {
res.end()
}
routes[route][method](req, res)
res.end()
} else {
notAvailable(method, route, res)
}
} else {
notAvailable(method, route, res)
}
})
server.listen(port, _callback(server))
}
module.exports = { newRoute, listen }