1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575<!--
* @Author: chenzhongsheng
* @Date: 2023-02-13 17:02:26
* @Description: Coding something
-->
<p align="center">
<img src='https://shiyix.cn/images/sener.png' width='120px'/>
</p>
<p align="center">
<a href="https://www.github.com/theajack/sener/stargazers" target="_black">
<img src="https://img.shields.io/github/stars/theajack/sener?logo=github" alt="stars" />
</a>
<a href="https://www.github.com/theajack/sener/network/members" target="_black">
<img src="https://img.shields.io/github/forks/theajack/sener?logo=github" alt="forks" />
</a>
<a href="https://www.npmjs.com/package/sener" target="_black">
<img src="https://img.shields.io/npm/v/sener?logo=npm" alt="version" />
</a>
<a href="https://www.npmjs.com/package/sener" target="_black">
<img src="https://img.shields.io/npm/dm/sener?color=%23ffca28&logo=npm" alt="downloads" />
</a>
<a href="https://www.jsdelivr.com/package/npm/sener" target="_black">
<img src="https://data.jsdelivr.com/v1/package/npm/sener/badge" alt="jsdelivr" />
</a>
</p>
<p align="center">
<a href="https://github.com/theajack" target="_black">
<img src="https://img.shields.io/badge/Author-%20theajack%20-7289da.svg?&logo=github" alt="author" />
</a>
<a href="https://www.github.com/theajack/sener/blob/master/LICENSE" target="_black">
<img src="https://img.shields.io/github/license/theajack/sener?color=%232DCE89&logo=github" alt="license" />
</a>
<a href="https://fastly.jsdelivr.net/gh/theajack/sener/dist/sener.latest.min.js"><img src="https://img.shields.io/bundlephobia/minzip/sener.svg" alt="Size"></a>
<a href="https://github.com/theajack/sener/search?l=javascript"><img src="https://img.shields.io/github/languages/top/theajack/sener.svg" alt="TopLang"></a>
<a href="https://github.com/theajack/sener/issues"><img src="https://img.shields.io/github/issues-closed/theajack/sener.svg" alt="issue"></a>
<a href="https://www.github.com/theajack/sener"><img src="https://img.shields.io/librariesio/dependent-repos/npm/sener.svg" alt="Dependent"></a>
</p>
<h3>🚀 Easy-to-use And Powerful nodejs http server</h3>
**[Online Demo](https://stackblitz.com/github/sener-js/sener-demo?file=index.js) | [Document](https://theajack.github.io/sener/) | [中文](https://github.com/theajack/sener/blob/master/README.cn.md) | [Update log](https://github.com/theajack/sener/blob/master/scripts/version.md) | [Feedback](https://github.com/theajack/sener/issues/new) | [Gitee](https://gitee.com/theajack/sener) | [Message Board](https://theajack.github.io/message-board/?app=sener)**
## 1. Features
1. Simple and efficient architecture, full TS writing, highly friendly TS declaration support
2. Support highly customized middleware system
3. Built-in router middleware
4. JSON middleware: Support JSON files for data storage
5. CORS middleware: supports cross-origin requests
6. Static Middleware: Support static file directories
7. Form middleware: Support formdata parsing and file upload
8. Config middleware: supports highly flexible parameter configuration and dynamic change and monitoring
9.log Middleware: Support flexible logging system and log level control
10. MySQL Middleware: Supports MySQL connections
11. Mongo middleware: Support for Mongo connections
12. RPC middleware: remote call support, support client and server use, support injection request X-trace-id
## 2. Basic Use
```
npm i sener
```
Simple demo:
```js
import {Sener} from 'sener';
new Sener();
```
options:
```js
new Sener({
port: 9000, // port: default value is 9000
middlewares: [], // Sener middlewares
});
```
### Best Practices
1. Use ebuild-cli
```
npm i ebuild-cli -g
```
```
ebuild init <Project name>
```
In the subsequent mode selection, select sener
2. Copy from [github address](https://github.com/theajack/sener-best-practice)
```
git clone https://github.com/theajack/sener-best-practice.git
```
## 3. Middlewares
### 3.1 router
Router middleware
```js
import {Sener, Router} from 'sener';
const router = new Router({
'/demo': ({ query }) => {
query.message = 'from get';
return { data: query };
},
'post:/demo': async ({ body }) => {
body.message = 'from post'
return { data: body };
},
});
new Sener({
middlewares: [router],
});
```
### 3.2 json
Use a JSON file as a database
```
npm i sener sener-json
```
```js
import {Sener, Router} from 'sener';
import {Json} from 'sener-json';
const router = new Router({
'/data': ({ query, read }) => {
// 'data' is json file name
return { data: read('data') };
},
'post:/data': async ({ body, write }) => {
const { data, save, id } = write('aa');
body.message = 'from post'
data.push({...body, id: id()}); // Add increment id
save(); // save it, THIS Method must be called
return { data };
},
});
new Sener({
middlewares: [router, new Json()],
});
```
options
```js
new Json({
dir: '', // directory for save json files. default value is ''
format: false, // Whether to format the JSON file. Default value: The development environment is false and the production environment is true
})
```
### 3.3 cors
Middleware that handles cross-origin requests
```
npm i sener sener-cors
```
```js
import {Sener, Cors} from 'sener';
new Sener({
middlewares: [new Cors()],
// new Cors(header); Or Set your custom headers
});
```
options
```js
new Cors({
// headers: Set your custom headers
})
```
### 3.4 static
Middleware that handles static resources
```
npm i sener sener-static
```
```js
import {Sener} from 'sener';
import {Static} from 'sener-static';
new Sener({
middlewares: [new Static()],
// new Static({dir}); dir default value is ./public
});
```
options
```js
new Static({
dir: './public', // Static directory, default value is ./public
})
```
### 3.5 form
Middleware that handles formdata and file uploads
```
npm i sener sener-form
```
```js
import {Sener, Router} from 'sener';
import {Form} from 'sener-form';
const router = new Router({
'post:/upload': ({ formData, files }) => {
return { formData, files }
},
});
new Sener({
middlewares: [new Form(), router],
// new Form({dir}); dir default value is ./public
});
```
options
```js
new Form({
dir: './public/upload', // File upload directory, default value is ./public/upload
})
```
### 3.6 log
```
npm i sener sener-log
```
Middleware that supports logging systems
```js
import {Sener, Router} from 'sener';
import {Log} from 'sener-log';
const router = new Router({
'get:/test': ({ query, logger }) => {
logger.log('msg', 'payload')
return { data: query }
},
});
new Sener({
middlewares: [new Log(), router],
});
```
typings
```ts
class Logger {
log(msg: string | IMessageData, payload?: any, type?: TLogType): void;
get traceid (): string;
refreshDurationStart(): void;
refreshTraceId(): void;
}
interface IMessageData {
msg?: string;
payload?: any;
type?: 'error' | 'log' | 'warn' | 'info';
level?: number;
extend?: object;
}
```
options
```js
new Log({
dir: '', // The directory where the log file is stored. default value is '', use root directory
useConsole: false, // Whether to enable console.log Print logs when the service is running. It is not recommended to turn on the production environment. default value is false
maxRecords: 10000, // The maximum number of stored records for a single log file , default value is 10000
level: -1, // The level of log printing, logs with a level less than this number will not be printed
// level?: (()=>number) Level can also be a method for dynamically obtaining level values, typically used in conjunction with config middleware
})
```
### 3.7 config
Middleware that supports flexible use of JSON configuration files
```
npm i sener sener-config
```
```js
import {Sener, Router} from 'sener';
import {Config} from 'sener-config';
const router = new Router({
'get:/test': ({ query, config, writeConfig, onConfigChange }) => {
const level = config.level;
level(); // read config
level(5); // write config
writeConfig('level', 5) // write config with writeConfig
onConfigChange(({key, value, prev}) => { // on config change
console.log(key, value, prev);
})
return { data: query }
},
});
const config = new Config();
// Use config instance
config.onConfigChange(({key, value, prev}) => { // on config change
console.log(key, value, prev);
});
config.data.level(); // read config
config.data.level(5); // write config
config.writeConfig('level', 2); // write config with writeConfig
new Sener({
middlewares: [config, router],
});
```
options
```ts
new Config({
dir: '', // directory for save config files. default value is ''
file: 'default', // file name of your config file. default value is 'default'
// file: ['c1', 'c2'], // Passing in an array indicates that multiple profiles are used
format: false, // Whether to format the JSON file. Default value: The development environment is false and the production environment is true
})
```
### 3.8 mysql
Middleware that supports mysql
```
npm i sener sener-mysql
```
```js
import {Sener, Router} from 'sener';
import {Mysql} from 'sener-mysql';
const router = new Router({
'get:/test': async ({ query, querySql, mysqlConn }) => {
const { results, fields } = await querySql('select * from user');
// Or use mysqlConn
return { data: query }
},
});
const mysql = new Mysql({
// Please refer to (https://www.npmjs.com/package/mysql) for details
})
mysql.connection;
new Sener({
middlewares: [mysql, router],
});
```
Please refer to [mysql](https://www.npmjs.com/package/mysql) for details
### 3.9 mongodb
Middleware that supports mongodb
```
npm i sener sener-mongodb
```
```js
import {Sener, Router} from 'sener';
import {Mongo} from 'sener-mongodb';
const router = new Router({
'get:/test': async ({ query, queryMongoDB, mongoClient }) => {
const {db, close} = await queryMongoDB('user');
// do something
// Or use mongoClient
return { data: query }
},
});
const mongodb = new Mongo({
// Please refer to (https://www.npmjs.com/package/mongodb) for details
});
mongodb.client;
new Sener({
middlewares: [mongodb, router],
});
```
Please refer to [mongodb](https://www.npmjs.com/package/mongodb) for details
### 3.10 rpc middleware
RPC middleware is used to make remote calls to services deployed on different servers or on different ports of the same server, allowing developers to initiate requests like remote services like function calls
It can also be used in scenarios where web clients initiate requests like the server
The middle will also inject an x-trace-id header into the request to ensure that the interface of the same access call has the same tracid, which can be used effectively with log middleware to locate the problem
```
npm i sener sener-rpc
```
#### 3.10.1 Server-side usage
1. Use configuration
```js
import {Sener, Router} from 'sener';
import {RPC} from 'sener-rpc';
const router = new Router({
'get:/test': async ({ query, rpc }) => {
const list = rpc.comment.get('/message', {page: 1}); // url and query
// use rpc.comment.request for more details
return { data: {query, list} }
},
});
new Sener({
middlewares: [new RPC({
user: 'http://localhost:3000', // The access base address of the user service
comment: 'http://localhost:3001', // The access base address of the comment service
}), router],
});
```
2. Use the createServices function
```js
import {Sener, Router} from 'sener';
import {RPC, Request} from 'sener-rpc';
class CommentRequest extends Request {
getList ({ app = 'common', index = 1 }: {
app?: string
index?: number
} = {}) {
return this.get('/message', {
app,
index,
size: 10,
});
}
}
function createServices(traceid = '') {
const base = (port: number) => `http://localhost:${port}`;
return {
comment: new CommentRequest({ base: base(3001), traceid }),
};
}
const router = new Router({
'get:/test': async ({ query, rpc }) => {
const list = rpc.comment.getList();
return { data: {query, list} }
},
});
new Sener({
middlewares: [new RPC(createServices), router],
});
```
#### 3.10.1 Client use
1. npm use
```
npm i sener-rpc
```
```js
import {WebRPC} from 'sener-rpc/dist/web.umd';
// 1. A single service can pass in the base address
const comment = new WebRPC('http://localhost:3001');
await comment.get('/message', {page: 1});
// 2. Multiple services pass into the map
const rpc = new WebRPC({
user: 'http://localhost:3000', // The access base address of the user service
comment: 'http://localhost:3001', // The access base address of the comment service
});
await rpc.comment.get('/message', {page: 1});
// 3. Use inheritance
class Comment extends WebRPC {
getList ({ app = 'common', index = 1 }: {
app?: string
index?: number
} = {}) {
return this.get('/message', {
app,
index,
size: 10,
});
}
}
await (new Comment()).getList();
```
2. cdn use
```html
<script src='https://cdn.jsdelivr.net/npm/sener-rpc'></script>
<script>
SenerRpc.WebRPC
</script>
```
## other
1. Dir
Sener stores all data files in ~/sener-data folder by default
```js
let BASE_SENER_DIR = path.resolve(homedir(), './sener-data')
```
If you want to modify,Please use the static property Dir
```ts
Sener.Dir = 'xxxxx'
```
2. Version
Get version
```ts
Sener.Version
```
## Custom Middleware
Documentation will continue to be improved
For now please refer to [middleware packages](https://github.com/theajack/sener/blob/master/packages)