turbo-express-js 1.0.0
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 +187 -0
- package/benchmarks/image.md +35 -0
- package/benchmarks/video.md +35 -0
- package/bin.test.js +59 -0
- package/express.test.js +73 -0
- package/index.js +3 -0
- package/index.test.js +116 -0
- package/lib/Router.js +100 -0
- package/lib/ServerCallback.js +65 -0
- package/lib/TurboServer.js +278 -0
- package/lib/enum/FileEndToContentType.js +35 -0
- package/lib/enum/ValidationMethodType.js +6 -0
- package/lib/middlewares/attachment_middleware.js +89 -0
- package/lib/middlewares/authentication_middleware.js +33 -0
- package/lib/middlewares/buildform_middleware.js +33 -0
- package/lib/middlewares/cors_middleware.js +33 -0
- package/lib/middlewares/runmethod_middleware.js +51 -0
- package/lib/middlewares/static_middleware.js +76 -0
- package/lib/middlewares/validation_middleware.js +39 -0
- package/lib/statictypes/RequestMethods.js +85 -0
- package/lib/types/FileType.js +55 -0
- package/lib/types/Middleware.js +59 -0
- package/lib/types/RequestInterface.js +398 -0
- package/lib/types/ResponseInterface.js +94 -0
- package/lib/types/ResponseMongo.js +35 -0
- package/lib/types/Route.js +97 -0
- package/lib/types/RunMethodType.js +49 -0
- package/lib/types/ValidationError.js +56 -0
- package/lib/types/ValidationSchema.js +139 -0
- package/lib/types/ValidationStaticTypes.js +34 -0
- package/lib/utils.js +111 -0
- package/package.json +38 -0
- package/samples/README.md +1 -0
- package/todo.md +11 -0
- package/version_management.md +13 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 breathfunwithmindte
|
|
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,187 @@
|
|
|
1
|
+
# TurboServer - A Fast and Powerful Node.js Web Framework
|
|
2
|
+
|
|
3
|
+
TurboServer is inspired by Express.js and offers a similar API to make it easy for developers familiar with Express to get started. In fact, TurboServer offers many of the same features and middleware that Express does, making it a great choice for developers who want a fast and efficient alternative to Express.
|
|
4
|
+
|
|
5
|
+
#
|
|
6
|
+
|
|
7
|
+
Additionally, TurboServer is built with an OOP architecture in mind, with Request and Response classes acting as interfaces. This makes it easy for developers to add new methods or override default functionality, leading to increased scalability and flexibility for your web applications.
|
|
8
|
+
|
|
9
|
+
#
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
To install TurboServer, simply run the following command:
|
|
14
|
+
|
|
15
|
+
```
|
|
16
|
+
npm install turboserver
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Getting Started
|
|
20
|
+
|
|
21
|
+
Here's a simple example of how to create a basic server using TurboServer:
|
|
22
|
+
|
|
23
|
+
``` javascript
|
|
24
|
+
|
|
25
|
+
const TurboServer = require('turboserver');
|
|
26
|
+
|
|
27
|
+
const app = new TurboServer(1);
|
|
28
|
+
|
|
29
|
+
app.get('/', (req, res) => {
|
|
30
|
+
res.send('Hello, World!');
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
app.listen(3000, () => {
|
|
34
|
+
console.log('Server started on port 3000');
|
|
35
|
+
});
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
# Clustering
|
|
39
|
+
|
|
40
|
+
TurboServer is built to support clustering out of the box. By passing a number to the TurboServer constructor, you can specify how many worker processes to spawn for your application.
|
|
41
|
+
|
|
42
|
+
In the example above, const app = new TurboServer(2) tells TurboServer to start two worker processes for the server. This allows the server to handle multiple requests simultaneously, improving the performance and scalability of your application.
|
|
43
|
+
|
|
44
|
+
To take full advantage of the clustering feature, it's important to make sure your application is stateless and doesn't rely on local variables or in-memory storage. With proper configuration, clustering can help your application handle a large number of requests with ease.
|
|
45
|
+
|
|
46
|
+
## Middlewares
|
|
47
|
+
|
|
48
|
+
TurboServer includes a number of built-in middlewares that you can use to add functionality to your application. Here are some of the most commonly used middlewares:
|
|
49
|
+
Static
|
|
50
|
+
|
|
51
|
+
The Static middleware serves static files from a specified folder:
|
|
52
|
+
|
|
53
|
+
``` javascript
|
|
54
|
+
app.use(TurboServer.Static({ folder: '/public' }));
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
# Upload
|
|
59
|
+
|
|
60
|
+
The Upload middleware handles file uploads:
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
``` javascript
|
|
64
|
+
app.post('/upload', TurboServer.Upload({ folder: '/storage' }));
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Validation
|
|
68
|
+
|
|
69
|
+
The Validation middleware validates incoming requests against a specified schema (can validate by namespaces<params, query, body>):
|
|
70
|
+
|
|
71
|
+
``` javascript
|
|
72
|
+
app.use(TurboServer.Validation({
|
|
73
|
+
validations: [
|
|
74
|
+
new TurboServer.ValidationSchema("username", [
|
|
75
|
+
{ name: TurboServer.ValidationMethodType.MINLENGTH, value: 3 },
|
|
76
|
+
{ name: TurboServer.ValidationMethodType.MAXLENGTH, value: 14 },
|
|
77
|
+
{ name: TurboServer.ValidationMethodType.ONEOF, value: ["Xristina"] }
|
|
78
|
+
], true, "Default Value", "body")
|
|
79
|
+
]
|
|
80
|
+
}));
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## Authentication
|
|
84
|
+
|
|
85
|
+
The Authentication middleware handles user authentication:
|
|
86
|
+
|
|
87
|
+
*work in progress still
|
|
88
|
+
|
|
89
|
+
``` javascript
|
|
90
|
+
app.use(TurboServer.Authentication());
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
## BuildForm
|
|
94
|
+
|
|
95
|
+
The BuildForm middleware parses some sort of model and can generate list of fields for external app (reactJS, nextJS) or for template engine like EJS:
|
|
96
|
+
|
|
97
|
+
*work in progress still
|
|
98
|
+
|
|
99
|
+
``` javascript
|
|
100
|
+
app.use(TurboServer.BuildForm());
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## Cors
|
|
104
|
+
|
|
105
|
+
The Cors middleware adds CORS headers to responses:
|
|
106
|
+
|
|
107
|
+
*work in progress still
|
|
108
|
+
|
|
109
|
+
``` javascript
|
|
110
|
+
app.use(TurboServer.Cors());
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
## Custom Middleware
|
|
115
|
+
|
|
116
|
+
In addition to the built-in middlewares, TurboServer allows you to create your own custom middlewares for your application. Custom middleware functions can be used to add functionality to your application that is specific to your needs.
|
|
117
|
+
|
|
118
|
+
To create a custom middleware function, simply define a function that takes three parameters: the request object, the response object, and the next function. The next function is a callback that is used to pass control to the next middleware function in the chain.
|
|
119
|
+
|
|
120
|
+
Here's an example of a custom middleware function that logs the request method and URL to the console:
|
|
121
|
+
|
|
122
|
+
``` javascript
|
|
123
|
+
function logger(req, res, next) {
|
|
124
|
+
console.log(`${req.method()} ${req.url()}`);
|
|
125
|
+
next();
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
app.use(logger);
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
In the above example, we define a logger function that logs the request method and URL to the console, and then calls the next function to pass control to the next middleware function in the chain. We then use the app.use method to add the logger function as a middleware function for all routes in our application.
|
|
132
|
+
|
|
133
|
+
You can define as many custom middleware functions as you need, and they will be executed in the order in which they are added to the middleware stack.
|
|
134
|
+
|
|
135
|
+
## Custom Middleware after controller
|
|
136
|
+
|
|
137
|
+
In TurboServer, you can define middleware that will run after the controller method has completed. This can be useful for tasks such as logging, error handling, or any other post-processing that needs to be done after the response has been sent.
|
|
138
|
+
|
|
139
|
+
To define an end middleware in TurboServer, you can use the useEndMiddleware method. This method takes a path pattern and one or more middleware functions as arguments. The middleware functions will be executed in the order they are passed to the method.
|
|
140
|
+
|
|
141
|
+
Here is an example of how to define an end middleware in TurboServer:
|
|
142
|
+
|
|
143
|
+
``` javascript
|
|
144
|
+
app.useEndMiddleware("/api/*", (req, res, next) => {
|
|
145
|
+
console.log("End middleware #1");
|
|
146
|
+
next();
|
|
147
|
+
}, (req, res, next) => {
|
|
148
|
+
console.log("End middleware #2");
|
|
149
|
+
next();
|
|
150
|
+
});
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
#
|
|
154
|
+
|
|
155
|
+
## Router
|
|
156
|
+
|
|
157
|
+
TurboServer offers a built-in Router to handle different HTTP methods and routes. The Router class is similar to the express.Router class, but it has some additional features. Here's a simple example of how to create a router and use it with TurboServer:
|
|
158
|
+
|
|
159
|
+
``` javascript
|
|
160
|
+
const TurboServer = require('turboserver');
|
|
161
|
+
|
|
162
|
+
const app = new TurboServer(2);
|
|
163
|
+
const router = new TurboServer.Router();
|
|
164
|
+
|
|
165
|
+
router.get("/admin/:username", (req, res) => { res.send(req.params().get("username")) });
|
|
166
|
+
|
|
167
|
+
app.use('/api', router);
|
|
168
|
+
|
|
169
|
+
app.listen(3000, () => {
|
|
170
|
+
console.log('Server started on port 3000');
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
## About the Author
|
|
176
|
+
|
|
177
|
+
Our lead developer is a top-tier expert web and software engineer with mastery in over 20 programming languages, including Java, PHP, JavaScript, Golang, C, C++, and Python. With years of experience, they have built numerous open-source projects in these languages, including web, mobile, and desktop applications, as well as libraries and frameworks. Their expertise and passion for technology have been the driving force behind the creation of TurboServer, and they continue to work tirelessly to make it the fastest and most efficient web framework available.
|
|
178
|
+
|
|
179
|
+
#
|
|
180
|
+
|
|
181
|
+
## Conclusion
|
|
182
|
+
|
|
183
|
+
TurboServer is a powerful and fast Node.js web framework that makes it easy to build high-performance web applications. With its built-in middlewares and simple API, it's a great choice for any project.
|
|
184
|
+
|
|
185
|
+
#
|
|
186
|
+
|
|
187
|
+
First deployment: 14/05/2023
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
|
|
2
|
+
# turbo express
|
|
3
|
+
|
|
4
|
+
//app.get("/public/something/*", TurboServer.Static({ folder: "/public", cache: true }))
|
|
5
|
+
|
|
6
|
+
oha http://localhost:5000/public/something/testass.jpg -n 1000
|
|
7
|
+
Summary:
|
|
8
|
+
Success rate: 1.0000
|
|
9
|
+
Total: 0.0666 secs
|
|
10
|
+
Slowest: 0.0492 secs
|
|
11
|
+
Fastest: 0.0001 secs
|
|
12
|
+
Average: 0.0029 secs
|
|
13
|
+
Requests/sec: 15018.7871
|
|
14
|
+
|
|
15
|
+
Total data: 104.55 MiB
|
|
16
|
+
Size/request: 107.06 KiB
|
|
17
|
+
Size/sec: 1.53 GiB
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
# expressjs
|
|
21
|
+
|
|
22
|
+
// app.use("/public/something/*", require("express").static(require("path").resolve() + "/public"))
|
|
23
|
+
|
|
24
|
+
oha http://localhost:5001/testass.jpg -n 1000
|
|
25
|
+
Summary:
|
|
26
|
+
Success rate: 1.0000
|
|
27
|
+
Total: 0.2664 secs
|
|
28
|
+
Slowest: 0.0755 secs
|
|
29
|
+
Fastest: 0.0017 secs
|
|
30
|
+
Average: 0.0131 secs
|
|
31
|
+
Requests/sec: 3754.2075
|
|
32
|
+
|
|
33
|
+
Total data: 104.55 MiB
|
|
34
|
+
Size/request: 107.06 KiB
|
|
35
|
+
Size/sec: 392.51 MiB
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
|
|
2
|
+
# turbo express
|
|
3
|
+
|
|
4
|
+
//app.get("/public/something/*", TurboServer.Static({ folder: "/public", cache: true }))
|
|
5
|
+
|
|
6
|
+
oha http://localhost:5000/public/something/sample.mp4 -n 1000
|
|
7
|
+
Summary:
|
|
8
|
+
Success rate: 1.0000
|
|
9
|
+
Total: 8.2990 secs
|
|
10
|
+
Slowest: 1.1018 secs
|
|
11
|
+
Fastest: 0.0914 secs
|
|
12
|
+
Average: 0.4102 secs
|
|
13
|
+
Requests/sec: 120.4966
|
|
14
|
+
|
|
15
|
+
Total data: 40.57 GiB
|
|
16
|
+
Size/request: 41.55 MiB
|
|
17
|
+
Size/sec: 4.89 GiB
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
# expressjs
|
|
21
|
+
|
|
22
|
+
// app.use("/public/something/*", require("express").static(require("path").resolve() + "/public"))
|
|
23
|
+
|
|
24
|
+
oha http://localhost:5001/sample.mp4 -n 1000
|
|
25
|
+
Summary:
|
|
26
|
+
Success rate: 1.0000
|
|
27
|
+
Total: 26.2146 secs
|
|
28
|
+
Slowest: 2.7239 secs
|
|
29
|
+
Fastest: 1.0497 secs
|
|
30
|
+
Average: 1.3067 secs
|
|
31
|
+
Requests/sec: 38.1467
|
|
32
|
+
|
|
33
|
+
Total data: 40.57 GiB
|
|
34
|
+
Size/request: 41.55 MiB
|
|
35
|
+
Size/sec: 1.55 GiB
|
package/bin.test.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
|
|
2
|
+
// ? testing with thousends of middleware
|
|
3
|
+
|
|
4
|
+
// for (let i = 0; i < 10000; i++) {
|
|
5
|
+
|
|
6
|
+
// app.use("/a/*", (req, res, next) => {
|
|
7
|
+
// next()
|
|
8
|
+
// req.setState("something", "helloow rold")
|
|
9
|
+
// console.log("something middleware is running 111");
|
|
10
|
+
// }, (req, res, next) => {
|
|
11
|
+
// next()
|
|
12
|
+
// req.setState("something", "helloow rold 222")
|
|
13
|
+
// console.log("something middleware is running 222");
|
|
14
|
+
// })
|
|
15
|
+
|
|
16
|
+
// }
|
|
17
|
+
|
|
18
|
+
// app.get("/a/hello", (req, res) => {
|
|
19
|
+
// res.send(req.useState("something"));
|
|
20
|
+
// })
|
|
21
|
+
|
|
22
|
+
// ! result: not cost any performance;\
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
// ? testing middleware after route
|
|
26
|
+
|
|
27
|
+
// app.use("/somepath", (req, res, next) => {console.log("wowoowow middleware is running"); next()});
|
|
28
|
+
|
|
29
|
+
// app.get("somepath", mycallaback)
|
|
30
|
+
|
|
31
|
+
// app.use("/somepath", (req, res, next) => {console.log("wowoowow middleware is running"); next()});
|
|
32
|
+
|
|
33
|
+
// ! result: middleware will not run if it set after the route\
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
// ! using runmethod to execute validations;
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
app.get("/hello1/:username",
|
|
46
|
+
TurboServer.RunMethod({ exe: [
|
|
47
|
+
{ belong: "req", methodname: "runmethod_addvalidation", inject: { validation: new TurboServer.ValidationSchema("username", [
|
|
48
|
+
{ name: TurboServer.ValidationMethodType.MINLENGTH, value: 3 },
|
|
49
|
+
{ name: TurboServer.ValidationMethodType.MAXLENGTH, value: 14 },
|
|
50
|
+
{ name: TurboServer.ValidationMethodType.ONEOF, value: ["XristinaMike"] }
|
|
51
|
+
], true, "hello", "params") } },
|
|
52
|
+
|
|
53
|
+
{ belong: "req", methodname: "execute_validation", inject: {}, isAsync: true }
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
] }),
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
hello);
|
package/express.test.js
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
const app = require("express")()
|
|
2
|
+
|
|
3
|
+
const cluster = require("cluster")
|
|
4
|
+
const router = require("express").Router();
|
|
5
|
+
|
|
6
|
+
if (cluster.isMaster) {
|
|
7
|
+
for (let i = 0; i < 7; i++) { cluster.fork() }
|
|
8
|
+
} else {
|
|
9
|
+
let p = "/"
|
|
10
|
+
//app.use(require("express").static(require("path").resolve() + "/public"));
|
|
11
|
+
console.log("running")
|
|
12
|
+
app.listen(5001)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
app.get("/hi", (req, res) => res.send("hi"));
|
|
16
|
+
|
|
17
|
+
return;
|
|
18
|
+
|
|
19
|
+
for (let i = 0; i < 11; i++) {
|
|
20
|
+
router.get(p, (req, res) => res.send(`Current ${i}`));
|
|
21
|
+
p = p + ":a/"
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
app.use("/public/something/*", require("express").static(require("path").resolve() + "/public"))
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
async function mycallaback (req, res) {
|
|
29
|
+
const r = req.body
|
|
30
|
+
|
|
31
|
+
res.status(201).json({ body: r, aq: [
|
|
32
|
+
{params: req.params, queries: req.query},{params: req.params, queries: req.query},{params: req.params, queries: req.query},
|
|
33
|
+
{params: req.params, queries: req.query},{params: req.params, queries: req.query},
|
|
34
|
+
{params: req.params, queries: req.query},{params: req.params, queries: req.query},{params: req.params, queries: req.query},
|
|
35
|
+
{params: req.params, queries: req.query},{params: req.params, queries: req.query},
|
|
36
|
+
{params: req.params, queries: req.query},{params: req.params, queries: req.query},{params: req.params, queries: req.query},
|
|
37
|
+
{params: req.params, queries: req.query},{params: req.params, queries: req.query},
|
|
38
|
+
{params: req.params, queries: req.query},{params: req.params, queries: req.query},{params: req.params, queries: req.query},
|
|
39
|
+
{params: req.params, queries: req.query},{params: req.params, queries: req.query},
|
|
40
|
+
{params: req.params, queries: req.query},{params: req.params, queries: req.query},
|
|
41
|
+
{params: req.params, queries: req.query},{params: req.params, queries: req.query},{params: req.params, queries: req.query},
|
|
42
|
+
{params: req.params, queries: req.query},{params: req.params, queries: req.query},
|
|
43
|
+
{params: req.params, queries: req.query},{params: req.params, queries: req.query},{params: req.params, queries: req.query},
|
|
44
|
+
{params: req.params, queries: req.query},{params: req.params, queries: req.query},
|
|
45
|
+
{params: req.params, queries: req.query},{params: req.params, queries: req.query},{params: req.params, queries: req.query},
|
|
46
|
+
{params: req.params, queries: req.query},{params: req.params, queries: req.query},
|
|
47
|
+
{params: req.params, queries: req.query},{params: req.params, queries: req.query},
|
|
48
|
+
{params: req.params, queries: req.query},{params: req.params, queries: req.query},{params: req.params, queries: req.query},
|
|
49
|
+
{params: req.params, queries: req.query},{params: req.params, queries: req.query},
|
|
50
|
+
{params: req.params, queries: req.query},{params: req.params, queries: req.query},{params: req.params, queries: req.query},
|
|
51
|
+
{params: req.params, queries: req.query},{params: req.params, queries: req.query},
|
|
52
|
+
{params: req.params, queries: req.query},{params: req.params, queries: req.query},{params: req.params, queries: req.query},
|
|
53
|
+
{params: req.params, queries: req.query},{params: req.params, queries: req.query},
|
|
54
|
+
] });
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
app.use(require("express").static("/public"));
|
|
58
|
+
|
|
59
|
+
app.get("/about/:something", mycallaback)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
app.use(require("express").json())
|
|
63
|
+
app.use((req, res, next) => next());
|
|
64
|
+
app.use((req, res, next) => next());
|
|
65
|
+
app.use((req, res, next) => next());
|
|
66
|
+
app.use((req, res, next) => next());
|
|
67
|
+
app.use((req, res, next) => next());
|
|
68
|
+
app.use((req, res, next) => next());
|
|
69
|
+
app.use((req, res, next) => next());
|
|
70
|
+
app.use(router);
|
|
71
|
+
|
|
72
|
+
}
|
|
73
|
+
|
package/index.js
ADDED
package/index.test.js
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
const TurboServer = require("./lib/TurboServer");
|
|
2
|
+
const { MongoClient } = require('mongodb');
|
|
3
|
+
|
|
4
|
+
const RequestInterface = require("./lib/types/RequestInterface");
|
|
5
|
+
const ResponseInterface = require("./lib/types/ResponseInterface");
|
|
6
|
+
const Router = require("./lib/Router");
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
*
|
|
12
|
+
* @param {RequestInterface} req
|
|
13
|
+
* @param {ResponseInterface} res
|
|
14
|
+
* @param {Function} next
|
|
15
|
+
*/
|
|
16
|
+
async function mymiddleware (req, res, next)
|
|
17
|
+
{
|
|
18
|
+
req.setState("testone", "some test here");
|
|
19
|
+
|
|
20
|
+
next();
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
const app = new TurboServer(2);
|
|
25
|
+
|
|
26
|
+
app.get("/public/*", TurboServer.Static({ folder: "/public", cache: true }));
|
|
27
|
+
app.post("/upload", TurboServer.Upload({ folder: "/storage", memory: true }));
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
app.use("/*", TurboServer.Validation({ validations: [
|
|
31
|
+
new TurboServer.ValidationSchema("username", [
|
|
32
|
+
{ name: TurboServer.ValidationMethodType.MINLENGTH, value: 3 },
|
|
33
|
+
{ name: TurboServer.ValidationMethodType.MAXLENGTH, value: 14 },
|
|
34
|
+
{ name: TurboServer.ValidationMethodType.ONEOF, value: ["XristinaMike"] }
|
|
35
|
+
], true, "hello", "body")
|
|
36
|
+
]}));
|
|
37
|
+
app.use("/*", TurboServer.Authentication());
|
|
38
|
+
app.use("/*", TurboServer.BuildForm());
|
|
39
|
+
app.use("/*", TurboServer.Cors());
|
|
40
|
+
|
|
41
|
+
// run method of request without running controller callback
|
|
42
|
+
app.get("/hello/:username", TurboServer.RunMethod({ exe: [{ belog: "req", methodname: "fetchUsers", isAsync: true, inject: { somedata: "hello world some data injected to that method" } }] }));
|
|
43
|
+
|
|
44
|
+
app.get("/", (req, res) => res.status(200).json({}))
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* @AUTHENTICATION MIDDLEWARE
|
|
48
|
+
* @BUILD FORM MIDDLEWARE
|
|
49
|
+
*/
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
*
|
|
54
|
+
* @param {RequestInterface} req
|
|
55
|
+
* @param {ResponseInterface} res
|
|
56
|
+
* @param {Function} next
|
|
57
|
+
*/
|
|
58
|
+
async function hello (req, res, next)
|
|
59
|
+
{
|
|
60
|
+
console.log(app.__link_routes = app.__link_routes.filter(f => f.path == "asdasd"))
|
|
61
|
+
console.log(app.__link_routes)
|
|
62
|
+
|
|
63
|
+
console.log("####", "hello")
|
|
64
|
+
console.log(req.validData)
|
|
65
|
+
next()
|
|
66
|
+
res.status(201).json({result: 1, something: req.validateDataObj(), app: req.instance});
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
app.useEndMiddleware("/*", (req, res, next) => {console.log("middleware running after the controller method - #1"); next()}, (req, res, next) => {console.log("last middleware #2"); next()});
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
app.use("/*", TurboServer.Cors({ }))
|
|
74
|
+
|
|
75
|
+
app.get("/a/ok", (req, res) => {
|
|
76
|
+
res.send("hello")
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
app.get("/hello/:username",
|
|
80
|
+
TurboServer.RunMethod({ exe: [{ belog: "req", methodname: "something", isAsync: true, inject: { somedata: "hello world some data" } }] }),
|
|
81
|
+
TurboServer.Validation({ validations: [
|
|
82
|
+
new TurboServer.ValidationSchema("username", [
|
|
83
|
+
{ name: TurboServer.ValidationMethodType.MINLENGTH, value: 3 },
|
|
84
|
+
{ name: TurboServer.ValidationMethodType.MAXLENGTH, value: 14 },
|
|
85
|
+
{ name: TurboServer.ValidationMethodType.ONEOF, value: ["XristinaMike"] }
|
|
86
|
+
], true, "hello", "params")
|
|
87
|
+
] }), hello);
|
|
88
|
+
|
|
89
|
+
app.get("/hello1/:username", hello);
|
|
90
|
+
|
|
91
|
+
app.link("/hello1/:username",
|
|
92
|
+
TurboServer.Validation({ validations: [
|
|
93
|
+
new TurboServer.ValidationSchema("username", [
|
|
94
|
+
{ name: TurboServer.ValidationMethodType.MINLENGTH, value: 3 },
|
|
95
|
+
{ name: TurboServer.ValidationMethodType.MAXLENGTH, value: 14 },
|
|
96
|
+
{ name: TurboServer.ValidationMethodType.ONEOF, value: ["XristinaMike"] }
|
|
97
|
+
], true, "hello", "params")
|
|
98
|
+
]}),
|
|
99
|
+
|
|
100
|
+
hello);
|
|
101
|
+
|
|
102
|
+
const admin_router = new TurboServer.Router();
|
|
103
|
+
|
|
104
|
+
admin_router.get("/admin/:username", (req, res) => { res.send(req.params().get("username")) });
|
|
105
|
+
|
|
106
|
+
app.use("/api/v1/", admin_router)
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
app.listen(5000);
|
|
110
|
+
|
|
111
|
+
app.logRoutes();
|
|
112
|
+
|
|
113
|
+
//console.log(app);
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
|
package/lib/Router.js
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* -->>-->>-->>-->>-->>-->>-->>-->>-->>-->>-->>-->>-->>-->>-->>-->>-->>-->>
|
|
3
|
+
*
|
|
4
|
+
* @PerfecTEvolutioN
|
|
5
|
+
* @MikeKarypidis
|
|
6
|
+
* @project - TurboServer
|
|
7
|
+
* @name TurboServer
|
|
8
|
+
* @license - MIT
|
|
9
|
+
* @copyright - ©2022 PerfectEvolution Corporation;
|
|
10
|
+
* @author - Mike Karypidis
|
|
11
|
+
* @version - 1.0.0
|
|
12
|
+
* @link - https://turboserverjs.org
|
|
13
|
+
* @github - https://github.com/breathfunwithmindte/turbo-server.git
|
|
14
|
+
*
|
|
15
|
+
* -->>-->>-->>-->>-->>-->>-->>-->>-->>-->>-->>-->>-->>-->>-->>-->>-->>-->>
|
|
16
|
+
*
|
|
17
|
+
* here the primary class of TurboExpress framework - TurboServer class;
|
|
18
|
+
*
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
const http = require("http");
|
|
22
|
+
const Router = require("./Router");
|
|
23
|
+
const RequestInterface = require("./types/RequestInterface");
|
|
24
|
+
const ResponseInterface = require("./types/ResponseInterface");
|
|
25
|
+
const { matching_route, matching_nonexact_route } = require("./utils");
|
|
26
|
+
const Route = require("./types/Route");
|
|
27
|
+
const Middleware = require("./types/Middleware");
|
|
28
|
+
const RequestMethods = require("./statictypes/RequestMethods");
|
|
29
|
+
const ServerCallback = require("./ServerCallback");
|
|
30
|
+
const static_middleware = require("./middlewares/static_middleware");
|
|
31
|
+
const TurboServer = require("./TurboServer");
|
|
32
|
+
|
|
33
|
+
module.exports = class Router
|
|
34
|
+
{
|
|
35
|
+
/**
|
|
36
|
+
* @typedef {Object} RouterMiddleware
|
|
37
|
+
* @property {String} path
|
|
38
|
+
* @property {Function[]} callbacks
|
|
39
|
+
*
|
|
40
|
+
* @typedef {Object} RouterRoute
|
|
41
|
+
* @property {String} path;
|
|
42
|
+
* @property {Function[]} callbacks;
|
|
43
|
+
* @property {String} method
|
|
44
|
+
*/
|
|
45
|
+
|
|
46
|
+
/** @type {String} path */
|
|
47
|
+
path = "";
|
|
48
|
+
|
|
49
|
+
/** @type{RouterRoute[]} routes */
|
|
50
|
+
routes = new Array();
|
|
51
|
+
|
|
52
|
+
/** @type{RouterMiddleware[]} middlewares */
|
|
53
|
+
middlewares = new Array();
|
|
54
|
+
|
|
55
|
+
/** @type{Middleware[]} __middlewares_begin */
|
|
56
|
+
__middlewares_begin = new Array(); // final middlewares - actually will be set after the final path is passed;
|
|
57
|
+
|
|
58
|
+
/** @param {string | null} path */
|
|
59
|
+
constructor (path) { this.path = path || ""; }
|
|
60
|
+
|
|
61
|
+
get(path, ...callbacks){ this.routes.push({ path: path, callbacks: callbacks, method: RequestMethods.GET }) }
|
|
62
|
+
post(path, ...callbacks){ this.routes.push({ path: path, callbacks: callbacks, method: RequestMethods.POST }) }
|
|
63
|
+
put(path, ...callbacks){ this.routes.push({ path: path, callbacks: callbacks, method: RequestMethods.PUT }) }
|
|
64
|
+
delete(path, ...callbacks){ this.routes.push({ path: path, callbacks: callbacks, method: RequestMethods.DELETE }) }
|
|
65
|
+
head(path, ...callbacks){ this.routes.push({ path: path, callbacks: callbacks, method: RequestMethods.HEAD }) }
|
|
66
|
+
patch(path, ...callbacks){ this.routes.push({ path: path, callbacks: callbacks, method: RequestMethods.PATCH }) }
|
|
67
|
+
options(path, ...callbacks){ this.routes.push({ path: path, callbacks: callbacks, method: RequestMethods.OPTIONS }) }
|
|
68
|
+
link(path, ...callbacks){ this.routes.push({ path: path, callbacks: callbacks, method: RequestMethods.LINK }) }
|
|
69
|
+
unlink(path, ...callbacks){ this.routes.push({ path: path, callbacks: callbacks, method: RequestMethods.UNLINK }) }
|
|
70
|
+
view(path, ...callbacks){ this.routes.push({ path: path, callbacks: callbacks, method: RequestMethods.VIEW }) }
|
|
71
|
+
lock(path, ...callbacks){ this.routes.push({ path: path, callbacks: callbacks, method: RequestMethods.LOCK }) }
|
|
72
|
+
unlock(path, ...callbacks){ this.routes.push({ path: path, callbacks: callbacks, method: RequestMethods.UNLOCK }) }
|
|
73
|
+
copy(path, ...callbacks){ this.routes.push({ path: path, callbacks: callbacks, method: RequestMethods.COPY }) }
|
|
74
|
+
purge(path, ...callbacks){ this.routes.push({ path: path, callbacks: callbacks, method: RequestMethods.PURGE }) }
|
|
75
|
+
propfind(path, ...callbacks){ this.routes.push({ path: path, callbacks: callbacks, method: RequestMethods.PROPFIND }) }
|
|
76
|
+
|
|
77
|
+
use(path, ...callbackOrRouter)
|
|
78
|
+
{
|
|
79
|
+
this.middlewares.push({path: path, callbacks: callbackOrRouter});
|
|
80
|
+
if (callbackOrRouter instanceof Router) throw new Error("Router use cannot accept another instance of Router.");
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* This method transfer routes/middlewares from router to the main application instance.
|
|
85
|
+
*
|
|
86
|
+
* @binding routers/middlewares to the main application;
|
|
87
|
+
* @param {String} path
|
|
88
|
+
* @param {TurboServer} self
|
|
89
|
+
*/
|
|
90
|
+
inject (path, self)
|
|
91
|
+
{
|
|
92
|
+
this.__middlewares_begin = this.middlewares.map(m => new Middleware(this.path + path + m.path, m.callbacks));
|
|
93
|
+
const rm = new RequestMethods();
|
|
94
|
+
rm.forEach((method) => {
|
|
95
|
+
const rts = this.routes.filter(f => f.method === method);
|
|
96
|
+
rts.map(froute => self[`__${method}_routes`].push(new Route(this.path + path + froute.path, method, froute.callbacks, this.__middlewares_begin, [])))
|
|
97
|
+
})
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
};
|