@radatek/microserver 2.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/dist/microserver.d.ts +769 -0
- package/dist/microserver.js +3001 -0
- package/package.json +30 -0
- package/readme.md +136 -0
package/package.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@radatek/microserver",
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"description": "HTTP MicroServer",
|
|
5
|
+
"author": "Darius Kisonas",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"http",
|
|
9
|
+
"microserver",
|
|
10
|
+
"server",
|
|
11
|
+
"httpserver"
|
|
12
|
+
],
|
|
13
|
+
"type": "module",
|
|
14
|
+
"main": "./dist/microserver.js",
|
|
15
|
+
"module": "./dist/microserver.js",
|
|
16
|
+
"types": "./dist/microserver.d.ts",
|
|
17
|
+
"files": [
|
|
18
|
+
"dist"
|
|
19
|
+
],
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "git+https://github.com/radateklt/microserver.git"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@types/node": "^22.14.1"
|
|
26
|
+
},
|
|
27
|
+
"scripts": {
|
|
28
|
+
"build": "pnpm --package typescript dlx tsc -d -m nodenext -t es2021 microserver.ts --outDir dist && sed -Ei 's/\\\"use strict\\\";\\n\\n//g' dist/microserver.js && sed -Ei ':a;N;$!ba;s/\\n +private _[^\\n]+//g;s/export \\{\\};//g' dist/microserver.d.ts"
|
|
29
|
+
}
|
|
30
|
+
}
|
package/readme.md
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
## HTTP MicroServer
|
|
2
|
+
|
|
3
|
+
Lightweight all-in-one http web server
|
|
4
|
+
|
|
5
|
+
Features:
|
|
6
|
+
- fast REST API router
|
|
7
|
+
- form/json body decoder
|
|
8
|
+
- file upload
|
|
9
|
+
- authentication
|
|
10
|
+
- plain/hashed passwords
|
|
11
|
+
- virtual hosts
|
|
12
|
+
- static files
|
|
13
|
+
- reverse proxy routes
|
|
14
|
+
- trust ip for reverse proxy
|
|
15
|
+
- json file storage with autosave
|
|
16
|
+
- websockets (single file module from ws package)
|
|
17
|
+
- tls with automatic certificate reload
|
|
18
|
+
- data model with validation and mongodb interface
|
|
19
|
+
- simple file/memory storage adapter for model
|
|
20
|
+
- promises as middleware
|
|
21
|
+
- controller class
|
|
22
|
+
- access rights per route
|
|
23
|
+
- access rights per model field
|
|
24
|
+
- rewrite
|
|
25
|
+
- redirect
|
|
26
|
+
- express like interface
|
|
27
|
+
|
|
28
|
+
### Usage examples:
|
|
29
|
+
|
|
30
|
+
Simple router:
|
|
31
|
+
|
|
32
|
+
```ts
|
|
33
|
+
const server = new MicroServer({
|
|
34
|
+
listen: 8080,
|
|
35
|
+
auth: {
|
|
36
|
+
users: {
|
|
37
|
+
usr: {
|
|
38
|
+
password: 'secret'
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
})
|
|
43
|
+
server.use('GET /api/hello/:id',
|
|
44
|
+
(req: ServerRequset, res: ServerResponse) =>
|
|
45
|
+
({message:'Hello ' + req.params.id + '!'}))
|
|
46
|
+
server.use('POST /api/login',
|
|
47
|
+
(req: ServerRequset, res: ServerResponse) =>
|
|
48
|
+
{
|
|
49
|
+
const user = await req.auth.login(req.body.user, req.body.password)
|
|
50
|
+
return user ? {user} : 403
|
|
51
|
+
})
|
|
52
|
+
server.use('GET /api/protected', 'acl:auth',
|
|
53
|
+
(req: ServerRequset, res: ServerResponse) =>
|
|
54
|
+
({message:'Secret resource'}))
|
|
55
|
+
server.use('static', {root:'public'})
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
Using `Controller` class:
|
|
59
|
+
|
|
60
|
+
```ts
|
|
61
|
+
class RestApi extends Controller {
|
|
62
|
+
static acl = '' // default acl
|
|
63
|
+
|
|
64
|
+
gethello(id) {
|
|
65
|
+
return {message:'Hello ' + id + '!'}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
async postlogin() {
|
|
69
|
+
const user = await this.auth.login(this.body.user, this.body.password)
|
|
70
|
+
return user ? {user} : 403
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
static 'acl:protected' = 'user'
|
|
74
|
+
static 'url:protected' = 'GET /protected'
|
|
75
|
+
protected() {
|
|
76
|
+
return {message:'Protected'}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const server = new MicroServer({
|
|
81
|
+
listen: 8080,
|
|
82
|
+
auth: {
|
|
83
|
+
users: {
|
|
84
|
+
usr: {
|
|
85
|
+
password: 'secret',
|
|
86
|
+
acl: {user: true}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
})
|
|
91
|
+
server.use('/api', RestApi)
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
`Model.handler` automatically detects usage for standard functions: get,insert,update,delete
|
|
95
|
+
|
|
96
|
+
```js
|
|
97
|
+
import { MicroServer, Model, MicroCollection, FileStore } from '@dariuski/microserver'
|
|
98
|
+
|
|
99
|
+
const usersCollection = new MicroCollection({ store: new FileStore({ dir: 'data' }), name: 'users' })
|
|
100
|
+
//const usersCollection = await db.collection('users')
|
|
101
|
+
|
|
102
|
+
const userProfile = new Model({
|
|
103
|
+
_id: 'string',
|
|
104
|
+
name: { type: 'string', required: true },
|
|
105
|
+
email: { type: 'string', format: 'email' },
|
|
106
|
+
password: { type: 'string', canRead: false },
|
|
107
|
+
role: { type: 'string' },
|
|
108
|
+
acl: { type: 'object' },
|
|
109
|
+
}, { collection: usersCollection, name: 'user' })
|
|
110
|
+
|
|
111
|
+
const server = new MicroServer({
|
|
112
|
+
listen:8080,
|
|
113
|
+
auth: {
|
|
114
|
+
users: (user, password) => userProfile.get(password ? {_id: user, password } : {_id: user})
|
|
115
|
+
}
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
userProfile.insert({name: 'admin', password: 'secret', role: 'admin', acl: {'user/*': true}})
|
|
119
|
+
|
|
120
|
+
server.use('POST /login', async (req) => {
|
|
121
|
+
const user = await req.auth.login(req.body.user, req.body.password)
|
|
122
|
+
return user ? { user } : 403
|
|
123
|
+
})
|
|
124
|
+
// authenticated user allways has auth access
|
|
125
|
+
server.use('GET /profile', 'acl:auth', req => ({ user: req.user }))
|
|
126
|
+
// get all users if role='admin'
|
|
127
|
+
server.use('GET /admin/users', 'role:admin', userProfile)
|
|
128
|
+
// get user by id if has acl 'user/get'
|
|
129
|
+
server.use('GET /admin/user/:id', 'acl:user/get', userProfile)
|
|
130
|
+
// insert new user if role='admin' and has acl 'user/insert'
|
|
131
|
+
server.use('POST /admin/user', 'role:admin', 'acl:user/insert', userProfile)
|
|
132
|
+
// update user if has acl 'user/update'
|
|
133
|
+
server.use('PUT /admin/user/:id', 'acl:user/update', userProfile)
|
|
134
|
+
// delete user if has acl 'user/update'
|
|
135
|
+
server.use('DELETE /admin/user/:id', 'acl:user/delete', userProfile)
|
|
136
|
+
```
|