redweb 0.1.1 → 0.1.3
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/LICENSE +21 -0
- package/README.md +176 -0
- package/index.js +13 -70
- package/package.json +6 -2
- package/index.test.js +0 -15
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Arkam Mazrui
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
# RedWeb
|
|
2
|
+
|
|
3
|
+
RedWeb is a simple and flexible Node.js framework built on top of Express.js. It allows you to quickly set up a web server with customizable options, including serving static files, defining custom API endpoints, and integrating WebSocket communication.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
To install RedWeb, use npm:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install redweb
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
### Basic Example
|
|
16
|
+
|
|
17
|
+
To get started with RedWeb, simply initialize your RedWeb instance with the default options:
|
|
18
|
+
|
|
19
|
+
```javascript
|
|
20
|
+
const { HttpServer } = require('redweb');
|
|
21
|
+
|
|
22
|
+
const app = new HttpServer();
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
### Custom Configuration
|
|
26
|
+
|
|
27
|
+
You can also configure RedWeb according to your needs:
|
|
28
|
+
|
|
29
|
+
```javascript
|
|
30
|
+
const { HttpServer, METHODS } = require('redweb');
|
|
31
|
+
|
|
32
|
+
const services = [
|
|
33
|
+
{
|
|
34
|
+
serviceName: '/submit-form',
|
|
35
|
+
method: METHODS.POST,
|
|
36
|
+
function: (req, res) => {
|
|
37
|
+
const { name, email, message } = req.body;
|
|
38
|
+
if (!name || !email || !message) {
|
|
39
|
+
return res.status(400).json({ error: 'All fields are required' });
|
|
40
|
+
}
|
|
41
|
+
// Implement your logic here
|
|
42
|
+
res.status(200).json({ success: 'Form submitted successfully' });
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
];
|
|
46
|
+
|
|
47
|
+
const options = {
|
|
48
|
+
port: 3000,
|
|
49
|
+
publicPaths: [
|
|
50
|
+
'./pages/my_public_html',
|
|
51
|
+
'./content/my_public_images',
|
|
52
|
+
'./styles/styles.css'
|
|
53
|
+
],
|
|
54
|
+
encoding: 'urlencoded',
|
|
55
|
+
services: services
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
const app = new HttpServer(options);
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### HTTPS Server
|
|
62
|
+
|
|
63
|
+
To create an HTTPS server, provide the SSL options including the paths to your SSL key and certificate files:
|
|
64
|
+
|
|
65
|
+
```javascript
|
|
66
|
+
const { HttpsServer } = require('redweb');
|
|
67
|
+
|
|
68
|
+
const options = {
|
|
69
|
+
port: 3443,
|
|
70
|
+
ssl: {
|
|
71
|
+
key: './path/to/key.pem',
|
|
72
|
+
cert: './path/to/cert.pem'
|
|
73
|
+
},
|
|
74
|
+
publicPaths: ['./public']
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
const app = new HttpsServer(options);
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### WebSocket Server
|
|
81
|
+
|
|
82
|
+
To create a WebSocket server, use the `SocketServer` function:
|
|
83
|
+
|
|
84
|
+
```javascript
|
|
85
|
+
const { SocketServer } = require('redweb');
|
|
86
|
+
|
|
87
|
+
const options = {
|
|
88
|
+
port: 3000,
|
|
89
|
+
connectionCallback: (socket) => {
|
|
90
|
+
console.log('New client connected');
|
|
91
|
+
socket.on('message', (data) => {
|
|
92
|
+
console.log('Message received:', data);
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
const socketServer = new SocketServer(options);
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### Secure WebSocket Server
|
|
101
|
+
|
|
102
|
+
To create a secure WebSocket server, provide the SSL options:
|
|
103
|
+
|
|
104
|
+
```javascript
|
|
105
|
+
const { SecureSocketServer } = require('redweb');
|
|
106
|
+
|
|
107
|
+
const options = {
|
|
108
|
+
port: 4443,
|
|
109
|
+
ssl: {
|
|
110
|
+
key: './path/to/key.pem',
|
|
111
|
+
cert: './path/to/cert.pem'
|
|
112
|
+
},
|
|
113
|
+
connectionCallback: (socket) => {
|
|
114
|
+
console.log('New client connected');
|
|
115
|
+
socket.on('message', (data) => {
|
|
116
|
+
console.log('Message received:', data);
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
const secureSocketServer = new SecureSocketServer(options);
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
## Options
|
|
125
|
+
|
|
126
|
+
### HTTP/HTTPS Options
|
|
127
|
+
|
|
128
|
+
The `HttpServer` and `HttpsServer` constructors accept an options object with the following properties:
|
|
129
|
+
|
|
130
|
+
- **port**: The port number to bind the server (default: `80`).
|
|
131
|
+
- **bind**: The bind address for the server (default: `0.0.0.0`).
|
|
132
|
+
- **publicPaths**: An array of paths to serve static files from (default: `['./public']`).
|
|
133
|
+
- **services**: An array of services with their endpoints and handlers (default: `[]`).
|
|
134
|
+
- **listenCallback**: A callback function to execute once the server starts listening (default: `undefined`).
|
|
135
|
+
- **encoding**: The encoding type for the request bodies. It can be either `'json'` or `'urlencoded'` (default: `'json'`).
|
|
136
|
+
- **ssl**: SSL configuration for the HTTPS server. It should include `key` and `cert` paths.
|
|
137
|
+
|
|
138
|
+
### WebSocket/Secure WebSocket Options
|
|
139
|
+
|
|
140
|
+
The `SocketServer` and `SecureSocketServer` constructors accept an options object with the following properties:
|
|
141
|
+
|
|
142
|
+
- **port**: The port number to bind the socket server (default: `3000`).
|
|
143
|
+
- **connectionCallback**: A callback function to execute once a client connects (default: `undefined`).
|
|
144
|
+
- **ssl**: SSL configuration for the secure WebSocket server. It should include `key` and `cert` paths.
|
|
145
|
+
|
|
146
|
+
### Example Options Object
|
|
147
|
+
|
|
148
|
+
```javascript
|
|
149
|
+
const options = {
|
|
150
|
+
port: 3000,
|
|
151
|
+
bind: '127.0.0.1',
|
|
152
|
+
publicPaths: ['./public', './assets'],
|
|
153
|
+
services: [
|
|
154
|
+
{
|
|
155
|
+
serviceName: '/api/data',
|
|
156
|
+
method: METHODS.GET,
|
|
157
|
+
function: (req, res) => {
|
|
158
|
+
res.json({ message: 'Hello, world!' });
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
],
|
|
162
|
+
listenCallback: () => console.log('Server is running...'),
|
|
163
|
+
encoding: 'json'
|
|
164
|
+
};
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
## Methods
|
|
168
|
+
|
|
169
|
+
RedWeb provides a `METHODS` object that contains the HTTP methods you can use for defining services:
|
|
170
|
+
|
|
171
|
+
- `METHODS.POST` - 'post'
|
|
172
|
+
- `METHODS.GET` - 'get'
|
|
173
|
+
|
|
174
|
+
## License
|
|
175
|
+
|
|
176
|
+
MIT License
|
package/index.js
CHANGED
|
@@ -1,70 +1,13 @@
|
|
|
1
|
-
const
|
|
2
|
-
const
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
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
|
-
|
|
23
|
-
const DEFAULT_OPTIONS = {
|
|
24
|
-
port: 80,
|
|
25
|
-
bind: '0.0.0.0',
|
|
26
|
-
publicPaths: ['./public'],
|
|
27
|
-
services: [],
|
|
28
|
-
listenCallback: undefined,
|
|
29
|
-
encoding: ENCODINGS.json
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* To get started with RedWeb, simply initialize your RedWeb instance:
|
|
34
|
-
* ```javascript
|
|
35
|
-
* const app = new RedWeb();
|
|
36
|
-
* ```
|
|
37
|
-
* You can also configure it according to your needs:
|
|
38
|
-
* ```javascript
|
|
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
|
-
* });
|
|
48
|
-
* ```
|
|
49
|
-
* @param {RedWebOptions} options - Configuration options for RedWeb.
|
|
50
|
-
* @return {RedWeb}
|
|
51
|
-
*/
|
|
52
|
-
function RedWeb(options = {}) {
|
|
53
|
-
options = { ...DEFAULT_OPTIONS, ...options };
|
|
54
|
-
const app = express();
|
|
55
|
-
const { publicPaths, port, listenCallback, services, encoding } = options;
|
|
56
|
-
|
|
57
|
-
// Middleware to parse request bodies based on the specified encoding
|
|
58
|
-
if (encoding === ENCODINGS.json) {
|
|
59
|
-
app.use(bodyParser.json());
|
|
60
|
-
} else if (encoding === ENCODINGS.urlencoded) {
|
|
61
|
-
app.use(bodyParser.urlencoded({ extended: true }));
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
services.forEach(service => app[service.method](service.serviceName, service.function));
|
|
65
|
-
publicPaths.forEach(public_path => app.use(express.static(path.join(process.cwd(), public_path))));
|
|
66
|
-
app.listen(port, listenCallback ? listenCallback : () => console.log(`RedWeb listening on port ${port}`));
|
|
67
|
-
return app;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
module.exports = { RedWeb, METHODS, DEFAULT_OPTIONS };
|
|
1
|
+
const { HttpServer, HttpsServer, ENCODINGS, METHODS, HTTP_OPTIONS } = require('./HttpServer');
|
|
2
|
+
const { SocketServer, SecureSocketServer, SOCKET_OPTIONS } = require('./SocketServer');
|
|
3
|
+
|
|
4
|
+
module.exports = {
|
|
5
|
+
HttpServer,
|
|
6
|
+
HttpsServer,
|
|
7
|
+
SocketServer,
|
|
8
|
+
SecureSocketServer,
|
|
9
|
+
ENCODINGS,
|
|
10
|
+
METHODS,
|
|
11
|
+
HTTP_OPTIONS,
|
|
12
|
+
SOCKET_OPTIONS
|
|
13
|
+
};
|
package/package.json
CHANGED
|
@@ -1,16 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "redweb",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "A way to quickly set up an express server",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"test": "jest"
|
|
8
8
|
},
|
|
9
|
+
"files": [
|
|
10
|
+
"index.js"
|
|
11
|
+
],
|
|
9
12
|
"keywords": [],
|
|
10
13
|
"author": "",
|
|
11
14
|
"license": "ISC",
|
|
12
15
|
"dependencies": {
|
|
13
|
-
"express": "^4.19.2"
|
|
16
|
+
"express": "^4.19.2",
|
|
17
|
+
"socket.io": "^4.7.5"
|
|
14
18
|
},
|
|
15
19
|
"devDependencies": {
|
|
16
20
|
"@types/express": "^4.17.21",
|
package/index.test.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
const { RedWeb } = require('.');
|
|
2
|
-
const path = require('path');
|
|
3
|
-
|
|
4
|
-
jest.mock('express', () => {
|
|
5
|
-
const self = () => ({
|
|
6
|
-
use: jest.fn(),
|
|
7
|
-
listen: jest.fn()
|
|
8
|
-
});
|
|
9
|
-
self.static = jest.fn();
|
|
10
|
-
return self;
|
|
11
|
-
})
|
|
12
|
-
|
|
13
|
-
test('it should create a RedWeb instance', () => {
|
|
14
|
-
expect(RedWeb()).toBeDefined();
|
|
15
|
-
});
|