binhend 1.1.6 → 1.1.8
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.
- package/package.json +1 -1
- package/src/api.js +34 -1
- package/src/binh.server.app.js +3 -1
- package/src/server.js +12 -0
package/package.json
CHANGED
package/src/api.js
CHANGED
|
@@ -43,6 +43,7 @@ function APIs(rootpath) {
|
|
|
43
43
|
const router = express.Router();
|
|
44
44
|
router.use(express.urlencoded({ extended: false }));
|
|
45
45
|
router.use(express.json());
|
|
46
|
+
router.use(parseAuthToken);
|
|
46
47
|
|
|
47
48
|
setTimeout(function() {
|
|
48
49
|
mapAPIs(rootpath, router);
|
|
@@ -51,6 +52,14 @@ function APIs(rootpath) {
|
|
|
51
52
|
return router;
|
|
52
53
|
}
|
|
53
54
|
|
|
55
|
+
function parseAuthToken(request, response, next) {
|
|
56
|
+
var authString = request.headers['authorization'];
|
|
57
|
+
if (typeof authString === 'string' && authString.startsWith('Bearer ')) {
|
|
58
|
+
request.token = authString.split('Bearer ')[1];
|
|
59
|
+
}
|
|
60
|
+
next();
|
|
61
|
+
}
|
|
62
|
+
|
|
54
63
|
function Router(module) {
|
|
55
64
|
var router = express.Router();
|
|
56
65
|
|
|
@@ -66,9 +75,33 @@ function Router(module) {
|
|
|
66
75
|
module.exports = router;
|
|
67
76
|
}
|
|
68
77
|
|
|
78
|
+
output.trycatch = trycatch;
|
|
79
|
+
|
|
69
80
|
return output;
|
|
70
81
|
}
|
|
71
82
|
|
|
83
|
+
function trycatch(callback, onerror) {
|
|
84
|
+
return async function(request, response, next) {
|
|
85
|
+
try {
|
|
86
|
+
return await callback(request, response, next);
|
|
87
|
+
}
|
|
88
|
+
catch (error) {
|
|
89
|
+
if (onerror instanceof Function) onerror(error);
|
|
90
|
+
else {
|
|
91
|
+
var info = typeof error.info === 'string' ? { error: error.info } : error.info;
|
|
92
|
+
response.status(error.httpCode || 500).json(info || { error: 'Internal Server Error' });
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function HttpError(httpCode, info) {
|
|
99
|
+
var error = new Error(info);
|
|
100
|
+
error.httpCode = httpCode;
|
|
101
|
+
error.info = info;
|
|
102
|
+
return error;
|
|
103
|
+
};
|
|
104
|
+
|
|
72
105
|
module.exports = {
|
|
73
|
-
APIs, Router
|
|
106
|
+
APIs, Router, HttpError
|
|
74
107
|
};
|
package/src/binh.server.app.js
CHANGED
|
@@ -3,7 +3,7 @@ const os = require('os');
|
|
|
3
3
|
const cluster = require('cluster');
|
|
4
4
|
const path = require('path');
|
|
5
5
|
const { Server } = require('./server');
|
|
6
|
-
const { APIs, Router } = require('./api');
|
|
6
|
+
const { APIs, Router, HttpError } = require('./api');
|
|
7
7
|
|
|
8
8
|
const maxcpus = os.cpus().length;
|
|
9
9
|
|
|
@@ -15,6 +15,8 @@ function ServerApp(binh, Binh, configloader) {
|
|
|
15
15
|
return Router(module);
|
|
16
16
|
};
|
|
17
17
|
|
|
18
|
+
binh.HttpError = HttpError;
|
|
19
|
+
|
|
18
20
|
Binh.api = function(apiPath) {
|
|
19
21
|
pathAPI = path.join(rootpath, apiPath);
|
|
20
22
|
return Binh;
|
package/src/server.js
CHANGED
|
@@ -8,6 +8,18 @@ function Server(options) {
|
|
|
8
8
|
const WEB_DIRECTORY = options.web;
|
|
9
9
|
const CORS = options.cors;
|
|
10
10
|
|
|
11
|
+
// Handle error of ExpressJS: URIError: Failed to decode param
|
|
12
|
+
// Error happens when add wrong '%' (e.i. invalid encoded URI component) into the request URL
|
|
13
|
+
server.use(function(req, res, next) {
|
|
14
|
+
try {
|
|
15
|
+
decodeURIComponent(req.path);
|
|
16
|
+
next();
|
|
17
|
+
}
|
|
18
|
+
catch(e) {
|
|
19
|
+
res.redirect('/');
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
|
|
11
23
|
if (CORS instanceof Function) {
|
|
12
24
|
server.use(CORS);
|
|
13
25
|
}
|