-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
221 lines (199 loc) · 6.83 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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
const http = require('http');
const fs = require('fs');
const path = require('path');
const mime = require('mime');
class Xcess {
/**
* Initialize the Xcess instance with empty routes and middlewares
*/
constructor() {
// Initialize the routes object with different HTTP methods
this.routes = {
GET: {},
POST: {},
PUT: {},
DELETE: {},
ALL: {}
}
// Initialize the middlewares array
this.middlewares = [];
}
/**
* Add a GET route handler
* @param {string} path - The route path
* @param {function} handler - The handler function for the route
*/
get(path, handler) {
this.routes.GET[path] = handler;
}
/**
* Add a POST route handler
* @param {string} path - The route path
* @param {function} handler - The handler function for the route
*/
post(path, handler) {
this.routes.POST[path] = handler;
}
/**
* Add a PUT route handler
* @param {string} path - The route path
* @param {function} handler - The handler function for the route
*/
put(path, handler) {
this.routes.PUT[path] = handler;
}
/**
* Add a DELETE route handler
* @param {string} path - The route path
* @param {function} handler - The handler function for the route
*/
delete(path, handler) {
this.routes.DELETE[path] = handler;
}
/**
* Add a route handler for all HTTP methods
* @param {string} path - The route path
* @param {function} handler - The handler function for the route
*/
all(path, handler) {
this.routes.ALL[path] = handler;
}
/**
* Add a middleware handler
* @param {string|function} path - The path or the middleware function
* @param {function} handler - The handler function for the middleware
*/
use(path, handler) {
if (typeof path === 'function') {
// If the path is a function, it is a global middleware
this.middlewares.push(path);
} else {
// If the path is a string, it is a path-based middleware
this.middlewares.push((req, res, next) => {
if (req.url.startsWith(path) || path === '/' || path === '*') {
handler(req, res, next);
} else {
next();
}
});
}
}
/**
* Handle Cross-Origin Resource Sharing (CORS)
* @param {object} options - The CORS options
* @returns {function} - The CORS middleware function
*/
static cors(options = {}) {
const defaultOptions = {
allowedOrigins: '*',
allowedMethods: 'GET, POST, PUT, DELETE',
allowedHeaders: 'Content-Type, Authorization',
};
const corsOptions = { ...defaultOptions, ...options };
return (req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', corsOptions.allowedOrigins); // Allow all origins
res.setHeader('Access-Control-Allow-Methods', corsOptions.allowedMethods); // Allow all methods
res.setHeader('Access-Control-Allow-Headers', corsOptions.allowedHeaders); // Allow all headers
res.setHeader('Access-Control-Max-Age', '86400'); // 24 hours
// Handle preflight request
if (req.method === 'OPTIONS') {
res.statusCode = 204;
res.end();
} else {
next();
}
};
}
/**
* Serve static files from a directory
* @param {string} dirPath - The directory path to serve static files from
* @returns {function} - The static file middleware function
*/
static static(dirPath) {
return (req, res, next) => {
const { url } = req;
const filePath = path.join(dirPath, url);
// Check if the file exists
fs.stat(filePath, (err, stats) => {
if (err) {
// If the file does not exist, invoke the next middleware
next();
} else {
// If the file exists, serve the file
const contentType = mime.getType(filePath);
res.setHeader('Content-Type', contentType);
const fileStream = fs.createReadStream(filePath);
fileStream.on('error', (err) => {
res.statusCode = 500;
res.end('Internal Server Error');
});
fileStream.pipe(res);
}
});
};
}
/**
* Route the request to the respective method handlers
* @private
* @param {http.IncomingMessage} req - request object
* @param {http.ServerResponse} res - response object
*/
#handleRequest(req, res) {
const { method, url } = req;
const routeHandler = this.routes[method][url] || this.routes.ALL[url];
if (routeHandler) {
routeHandler(req, res);
} else {
res.statusCode = 404;
res.end(`Cannot ${method} ${url}`);
}
}
/**
* Handle the middleware stack
* @private
* @param {http.IncomingMessage} req - request object
* @param {http.ServerResponse} res - response object
*/
#handleMiddlewares(req, res) {
const next = () => {
this.#handleMiddlewares(req, res);
};
const currentMiddleware = this.middlewares.shift();
if (currentMiddleware) {
currentMiddleware(req, res, next);
} else {
this.#handleRequest(req, res);
}
}
/**
* Start listening on the specified port with the given callback
* @param {number} port - The port number to listen on
* @param {function} callback - The callback function to be executed on successful server start
*/
listen(port, callback) {
const server = http.createServer((req, res) => {
// Add the status method to the response object
res.status = (code) => {
res.statusCode = code;
return res;
};
// Add the json method to the response object
res.json = (data) => {
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(data));
};
// Add the send method to the response object
res.send = (body) => {
if (typeof body === 'object') {
res.json(body);
} else {
res.setHeader('Content-Type', 'text/plain');
res.end(body);
}
};
this.#handleMiddlewares(req, res);
});
server.listen(port, callback);
}
}
module.exports = Xcess;