redweb 0.0.8 → 0.0.9

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/index.js +44 -25
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -2,45 +2,64 @@ const express = require('express');
2
2
  const bodyParser = require('body-parser');
3
3
  const path = require('path');
4
4
 
5
+ /**
6
+ * @typedef {'json' | 'urlencoded'} RedWebEncoding
7
+ */
8
+
9
+ /**
10
+ * RedWeb options object.
11
+ * @typedef {Object} RedWebOptions
12
+ * @property {number} [port=80] - The port number to bind the server.
13
+ * @property {string} [bind='0.0.0.0'] - The bind address for the server.
14
+ * @property {string[]} [publicPaths=['./public']] - An array of paths to serve static files from.
15
+ * @property {Array<{serviceName: string, method: string, function: Function}>} [services=[]] - An array of services with their endpoints and handlers.
16
+ * @property {Function} [listenCallback] - Callback function to execute once the server starts listening.
17
+ * @property {RedWebEncoding} [encoding='json'] - The encoding type for the request bodies ('json' or 'urlencoded').
18
+ */
19
+
20
+ const ENCODINGS = { json: 'json', urlencoded: 'urlencoded' };
21
+ const METHODS = { POST: 'post', GET: 'get' };
22
+
5
23
  const DEFAULT_OPTIONS = {
6
24
  port: 80,
7
25
  bind: '0.0.0.0',
8
26
  publicPaths: ['./public'],
9
27
  services: [],
10
- listenCallback: undefined
11
- }
12
-
13
- const METHODS = {'POST': 'post', 'GET': 'get'}
28
+ listenCallback: undefined,
29
+ encoding: ENCODINGS.json
30
+ };
14
31
 
15
32
  /**
16
33
  * To get started with RedWeb, simply initialize your RedWeb instance:
17
34
  * ```javascript
18
- * const app = new RedWeb();
35
+ * const app = new RedWeb();
19
36
  * ```
20
37
  * You can also configure it according to your needs:
21
38
  * ```javascript
22
- * const app = new RedWeb({
23
- * port: 3000,
24
- * public: [
25
- * './pages/my_public_html',
26
- * './content/my_public_images',
27
- * './styles/styles.css']
28
- * }
29
- * );
39
+ * const app = new RedWeb({
40
+ * port: 3000,
41
+ * publicPaths: [
42
+ * './pages/my_public_html',
43
+ * './content/my_public_images',
44
+ * './styles/styles.css'
45
+ * ],
46
+ * encoding: 'urlencoded'
47
+ * });
30
48
  * ```
31
- *
32
- * @param {{port: Number, bind: String, publicPaths: Array<String>, services: Array<{serviceName: String, method: String, function: Function}>}} options
33
- * @return RedWeb
49
+ * @param {RedWebOptions} options - Configuration options for RedWeb.
50
+ * @return {RedWeb}
34
51
  */
35
- function RedWeb(options) {
36
- options = {...DEFAULT_OPTIONS, ...options};
52
+ function RedWeb(options = {}) {
53
+ options = { ...DEFAULT_OPTIONS, ...options };
37
54
  const app = express();
38
- const { publicPaths, port, listenCallback, services } = options;
39
- app.use(bodyParser.json());
40
- services.forEach((service) => app[service.method](service.serviceName, service.function));
41
- publicPaths.forEach((public_path) => app.use(express.static(path.join(process.cwd(), public_path))));
42
- app.listen(port, listenCallback ? listenCallback : () => console.log('RedWeb listening on port ' + options.port));
43
- return this;
55
+ const { publicPaths, port, listenCallback, services, encoding } = options;
56
+
57
+ app.use(bodyParser[encoding]);
58
+
59
+ services.forEach(service => app[service.method](service.serviceName, service.function));
60
+ publicPaths.forEach(public_path => app.use(express.static(path.join(process.cwd(), public_path))));
61
+ app.listen(port, listenCallback ? listenCallback : () => console.log(`RedWeb listening on port ${port}`));
62
+ return app;
44
63
  }
45
64
 
46
- module.exports = { RedWeb, METHODS, DEFAULT_OPTIONS }
65
+ module.exports = { RedWeb, METHODS, DEFAULT_OPTIONS };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "redweb",
3
- "version": "0.0.8",
3
+ "version": "0.0.9",
4
4
  "description": "A way to quickly set up an express server",
5
5
  "main": "index.js",
6
6
  "scripts": {