binhend 1.1.3 → 1.1.5
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/example_api/index.js +1 -0
- package/index.js +2 -2
- package/package.json +1 -1
- package/src/binh.server.app.js +14 -8
- package/src/cors.js +17 -0
- package/src/server.js +11 -1
package/example_api/index.js
CHANGED
package/index.js
CHANGED
package/package.json
CHANGED
package/src/binh.server.app.js
CHANGED
|
@@ -8,15 +8,20 @@ const { APIs, Router } = require('./api');
|
|
|
8
8
|
const maxcpus = os.cpus().length;
|
|
9
9
|
|
|
10
10
|
function ServerApp(binh, Binh, configloader) {
|
|
11
|
-
var
|
|
12
|
-
|
|
11
|
+
var rootpath = Binh.getRootpath(),
|
|
12
|
+
forkcount = 0, enabledCORS, pathAPI, portNumber;
|
|
13
13
|
|
|
14
14
|
binh.router = function(module) {
|
|
15
15
|
return Router(module);
|
|
16
16
|
};
|
|
17
17
|
|
|
18
|
-
Binh.api = function(
|
|
19
|
-
|
|
18
|
+
Binh.api = function(apiPath) {
|
|
19
|
+
pathAPI = path.join(rootpath, apiPath);
|
|
20
|
+
return Binh;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
Binh.cors = function(input) {
|
|
24
|
+
enabledCORS = input instanceof Function ? input : (input === false ? false : true);
|
|
20
25
|
return Binh;
|
|
21
26
|
};
|
|
22
27
|
|
|
@@ -33,10 +38,10 @@ function ServerApp(binh, Binh, configloader) {
|
|
|
33
38
|
|
|
34
39
|
Binh.port = function(port) {
|
|
35
40
|
if (typeof port === 'number') {
|
|
36
|
-
|
|
41
|
+
portNumber = port;
|
|
37
42
|
}
|
|
38
43
|
else if (typeof port === 'string') {
|
|
39
|
-
|
|
44
|
+
portNumber = binh.config(port);
|
|
40
45
|
}
|
|
41
46
|
return Binh;
|
|
42
47
|
};
|
|
@@ -55,8 +60,9 @@ function ServerApp(binh, Binh, configloader) {
|
|
|
55
60
|
function launch(callback) {
|
|
56
61
|
configloader.done(function(configs) {
|
|
57
62
|
new Server({
|
|
58
|
-
port:
|
|
59
|
-
api: APIs(
|
|
63
|
+
port: portNumber,
|
|
64
|
+
api: APIs(pathAPI),
|
|
65
|
+
cors: enabledCORS,
|
|
60
66
|
web: Binh.web.value,
|
|
61
67
|
callback: callback,
|
|
62
68
|
configs: configs
|
package/src/cors.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
|
|
2
|
+
function defaultCORS(req, res, next) {
|
|
3
|
+
res.header('Access-Control-Allow-Origin', req.get('Origin') || '*');
|
|
4
|
+
res.header('Access-Control-Allow-Credentials', 'true');
|
|
5
|
+
res.header('Access-Control-Allow-Methods', 'GET,HEAD,PUT,PATCH,POST,DELETE');
|
|
6
|
+
res.header('Access-Control-Expose-Headers', 'Content-Length');
|
|
7
|
+
res.header('Access-Control-Allow-Headers', 'Accept, Authorization, Content-Type, X-Requested-With, Range');
|
|
8
|
+
if (req.method === 'OPTIONS') {
|
|
9
|
+
return res.send(200);
|
|
10
|
+
} else {
|
|
11
|
+
return next();
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
module.exports = {
|
|
16
|
+
defaultCORS
|
|
17
|
+
};
|
package/src/server.js
CHANGED
|
@@ -1,9 +1,19 @@
|
|
|
1
|
+
|
|
1
2
|
const express = require('express');
|
|
3
|
+
const { defaultCORS } = require('./cors');
|
|
2
4
|
|
|
3
5
|
function Server(options) {
|
|
4
6
|
const server = express();
|
|
5
7
|
const PORT = options.port || 1300;
|
|
6
8
|
const WEB_DIRECTORY = options.web;
|
|
9
|
+
const CORS = options.cors;
|
|
10
|
+
|
|
11
|
+
if (CORS instanceof Function) {
|
|
12
|
+
server.use(CORS);
|
|
13
|
+
}
|
|
14
|
+
else if (CORS === true) {
|
|
15
|
+
server.use(defaultCORS);
|
|
16
|
+
}
|
|
7
17
|
|
|
8
18
|
if (WEB_DIRECTORY) {
|
|
9
19
|
server.use(express.static(WEB_DIRECTORY));
|
|
@@ -16,7 +26,7 @@ function Server(options) {
|
|
|
16
26
|
if (WEB_DIRECTORY) {
|
|
17
27
|
server.get('/*', (req, res) => res.sendFile('index.html', { root: WEB_DIRECTORY }));
|
|
18
28
|
}
|
|
19
|
-
|
|
29
|
+
|
|
20
30
|
server.listen(PORT, function() {
|
|
21
31
|
console.log(`[NODE] Server is listening on http://localhost:${PORT}/`);
|
|
22
32
|
|