cpeak 1.2.0 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/lib/index.js +18 -3
  2. package/package.json +1 -1
package/lib/index.js CHANGED
@@ -7,6 +7,7 @@ class CPeak {
7
7
  this.server = http.createServer();
8
8
  this.routes = {};
9
9
  this.middleware = [];
10
+ this.handleErr;
10
11
 
11
12
  this.server.on("request", (req, res) => {
12
13
  // Send a file back to the client
@@ -32,18 +33,28 @@ class CPeak {
32
33
  res.end(JSON.stringify(data));
33
34
  };
34
35
 
36
+ const urlWithoutParams = req.url.split("?")[0];
37
+ req.params = new URLSearchParams(req.url.split("?")[1]);
38
+
35
39
  // Run all the middleware functions before we run the corresponding route
36
40
  const runMiddleware = (req, res, middleware, index) => {
37
41
  // Out exit point...
38
42
  if (index === middleware.length) {
39
43
  // If the routes object does not have a key of req.method + req.url, return 404
40
- if (!this.routes[req.method.toLocaleLowerCase() + req.url]) {
44
+ if (!this.routes[req.method.toLocaleLowerCase() + urlWithoutParams]) {
41
45
  return res
42
46
  .status(404)
43
- .json({ error: `Cannot ${req.method} ${req.url}` });
47
+ .json({ error: `Cannot ${req.method} ${urlWithoutParams}` });
44
48
  }
45
49
 
46
- this.routes[req.method.toLowerCase() + req.url](req, res);
50
+ this.routes[req.method.toLowerCase() + urlWithoutParams](
51
+ req,
52
+ res,
53
+ (error) => {
54
+ res.setHeader("Connection", "close");
55
+ this.handleErr(error, req, res);
56
+ }
57
+ );
47
58
  } else {
48
59
  middleware[index](req, res, () => {
49
60
  runMiddleware(req, res, middleware, index + 1);
@@ -63,6 +74,10 @@ class CPeak {
63
74
  this.middleware.push(cb);
64
75
  }
65
76
 
77
+ handleErr(cb) {
78
+ this.handleErr = cb;
79
+ }
80
+
66
81
  listen(port, cb) {
67
82
  this.server.listen(port, () => {
68
83
  cb();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cpeak",
3
- "version": "1.2.0",
3
+ "version": "1.4.0",
4
4
  "description": "",
5
5
  "main": "./lib/index.js",
6
6
  "scripts": {