-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
137 lines (120 loc) · 3.77 KB
/
index.js
File metadata and controls
137 lines (120 loc) · 3.77 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
const http = require('http');
const url = require('url');
const path = require('path');
const addCustomResMethods = require('./lib/res.methods');
const { static, errors: { defaultResponse } } = require('./lib/middlewares');
const { getParams,
getQueryObj,
PathObject } = require('./lib/url_path');
function Hexpress() {
this.middlewares = [];
this.errorMiddlewares = [defaultResponse];
this.settings = {
views: path.join(process.cwd(), 'views')
};
}
Hexpress.static = static;
Hexpress.prototype.get = function (path, handler) {
this.middlewares.push({
type: 'route',
path: new PathObject(path, 'GET'),
handler
});
};
Hexpress.prototype.post = function (path, handler) {
this.middlewares.push({
type: 'route',
path: new PathObject(path, 'POST'),
handler
});
};
Hexpress.prototype.put = function (path, handler) {
this.middlewares.push({
type: 'route',
path: new PathObject(path, 'PUT'),
handler
});
};
Hexpress.prototype.delete = function (path, handler) {
this.middlewares.push({
type: 'route',
path: new PathObject(path, 'DELETE'),
handler
});
};
Hexpress.prototype.all = function (path, handler) {
this.middlewares.push({
type: 'route',
path: new PathObject(path, 'ALL'),
handler
});
};
Hexpress.prototype.use = function (mw) {
if (mw.length === 4) this.errorMiddlewares.splice(this.errorMiddlewares.length - 1, 0, mw);
else {
this.middlewares.push({
type: 'custom',
path: null,
handler: mw
});
}
};
Hexpress.prototype.set = function (prop, name) {
if (prop === 'view engine') {
this.settings['view engine'] = require(name);
} else if (prop === 'views') {
this.settings.views = path.resolve(process.cwd(), name);
}
};
const defaultError = {
error: 'Internal server error'
};
Hexpress.prototype.listen = function (...args) {
const nodeServer = http.createServer((req, res) => {
let { pathname, query } = url.parse(req.url);
if (query) {
req.query = getQueryObj(query);
}
const method = req.method;
res = addCustomResMethods(res, this.settings);
const tryMiddlewares = (middlewares, req, res, err = defaultError) => {
if (middlewares.length === 0) return tryErrorMiddlewares(this.errorMiddlewares, err, req, res);
const mw = middlewares[0];
if (mw.type === 'custom') {
mw.handler(req, res, (err) => {
if (err) return tryMiddlewares([], req, res, err);
return tryMiddlewares(middlewares.slice(1), req, res);
});
} else if (mw.path && mw.path.method === 'ALL' && mw.path.path === pathname) {
return mw.handler(req, res, (err) => {
return tryMiddlewares([], req, res, err);
});
}
else if (mw.path && mw.path.method === method) {
if (!mw.path.parameterized && (mw.path.path === pathname)) return mw.handler(req, res, (err) => {
if (err) tryMiddlewares([], req, res, err);
});
else if (!mw.path.parameterized && (mw.path.path !== pathname)) return tryMiddlewares(middlewares.slice(1), req, res);
else if (mw.path.regex.test(pathname)) {
const params = getParams(mw.path.fragments, pathname);
req.params = params;
return mw.handler(req, res, (err) => {
if (err) tryMiddlewares([], req, res, err);
});
}
} else {
return tryMiddlewares(middlewares.slice(1), req, res);
}
};
const tryErrorMiddlewares = (errorMiddlewares, err, req, res) => {
let mw = errorMiddlewares[0];
mw(err, req, res, (error = err) => {
tryErrorMiddlewares(errorMiddlewares.slice(1), error, req, res);
});
};
tryMiddlewares(this.middlewares, req, res);
});
nodeServer.listen(...args);
return nodeServer;
};
module.exports = Hexpress;