-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminiExpress.js
More file actions
125 lines (117 loc) · 4.01 KB
/
Copy pathminiExpress.js
File metadata and controls
125 lines (117 loc) · 4.01 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
import http from 'http'
import { notFound } from './defaults.js';
import { handleQueryParams } from './utils.js';
import { renderStatic } from './utils.js';
export class Router {
constructor() {
this.middlewares = []
this.routes = {
"GET": {}, "POST": {}, 'PATCH': {}, 'DELETE': {}
}
this.dynamicRoutes = {
"GET": {}, "POST": {}, 'PATCH': {}, 'DELETE': {}
}
}
get(route, ...callbacks) {
if (route.includes(':')) {
this.dynamicRoutes['GET'][route] = [...this.middlewares, ...callbacks];
}
else {
this.routes['GET'][route] = [...this.middlewares, ...callbacks];
}
}
post(route, ...callbacks) {
this.routes['POST'][route] = [...this.middlewares, ...callbacks];
}
patch(route, callback) {
this.routes['PATCH'][route] = callback;
}
}
export default class MiniExpress extends Router {
constructor() {
super()
this.staticFolder = ''
}
register(route, routePool) {
for (let method in routePool.routes) {
for (let endpoint in routePool.routes[method]) {
this[method.toLowerCase()](`${route}${endpoint}`, ...routePool.routes[method][endpoint])
}
}
for (let method in routePool.dynamicRoutes) {
for (let endpoint in routePool.dynamicRoutes[method]) {
this[method.toLowerCase()](`${route}${endpoint}`, ...routePool.dynamicRoutes[method][endpoint])
}
}
}
use(...args) {
if (typeof args[0] === 'string') {
this.register(args[0], args?.[1])
}
else {
this.middlewares.push(...args)
}
// console.log(this.dynamicRoutes)
}
redirect(res, location) {
res.writeHead(302, { Location: location })
return res.end()
}
async handleForm(req) {
return new Promise((resolve, reject) => {
let body = ''
req.on('data', chunk => body += chunk)
req.on('end', () => {
req.form = body
body = handleQueryParams(body)
resolve(body)
})
req.on('error', error => reject(error))
})
}
listen(port, callback) {
http.createServer((req, res) => {
this.routeHandler(req, res)
}).listen(port, callback)
}
handleDynamicRoute(path, method, req) {
let methodRoutes = this.dynamicRoutes?.[method]
const pathSegemts = path.split('/')
for (var route in methodRoutes) {
let patternSegments = route.split('/')
if (patternSegments.length !== pathSegemts.length) continue
var params = ''
for (let index = 0; index < pathSegemts.length; index++) {
if (patternSegments[index].startsWith(':')) {
params += `${patternSegments[index].substring(1)}=${pathSegemts[index]}&`
} else if (patternSegments[index] !== pathSegemts[index]) break
if (index == patternSegments.length - 1) {
req.params = handleQueryParams(params)
return methodRoutes[route]
}
}
}
return null
}
serveStatic(req, res) {
return renderStatic(`./templates/${req.url}`, res)
}
routeHandler(req, res) {
if (this.staticFolder && req.url.startsWith(this.staticFolder)) {
return res.end(this.serveStatic(req, res))
}
let [path, queryparams] = req.url.split('?')
if (queryparams) handleQueryParams(queryparams, req)
// const callbacks = this.routes?.[req.method]?.[path]
const callbacks = this.routes?.[req.method]?.[path] ?? this.handleDynamicRoute(path, req.method, req)
if (callbacks) {
for (let callback of callbacks) {
if (callback(req, res)) break
}
return
}
else {
return res.end(notFound(res))
}
}
}