binhend 1.1.4 → 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.
@@ -7,6 +7,7 @@ new Binh()
7
7
  .global('config', binh.config)
8
8
  .config('config')
9
9
  .api('routes')
10
+ .cors()
10
11
  .port('PORT')
11
12
  .cluster.fork(0)
12
13
  .start(function(server, configs) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "binhend",
3
- "version": "1.1.4",
3
+ "version": "1.1.5",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "author": "Nguyen Duc Binh",
@@ -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 forkcount = 0,
12
- rootpath = Binh.getRootpath();
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(api_path) {
19
- Binh.api.value = path.join(rootpath, api_path);
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
- Binh.port.value = port;
41
+ portNumber = port;
37
42
  }
38
43
  else if (typeof port === 'string') {
39
- Binh.port.value = binh.config(port);
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: Binh.port.value,
59
- api: APIs(Binh.api.value),
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