kempo-server 1.10.6 → 1.10.7
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/llm.txt +140 -0
- package/package.json +1 -3
package/llm.txt
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
# kempo-server
|
|
2
|
+
|
|
3
|
+
Zero-dependency, file-based routing Node.js HTTP server.
|
|
4
|
+
|
|
5
|
+
## Install & Run
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
npm install kempo-server
|
|
9
|
+
npx kempo-server -r ./my-root -p 3000
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
Flags: `--root`/`-r` (default `./`), `--port`/`-p` (default `3000`), `--logging`/`-l` (0-4 or silent/minimal/verbose/debug, default 2), `--config`/`-c` path to config file (default `.config.json`).
|
|
13
|
+
|
|
14
|
+
## File-Based Routing
|
|
15
|
+
|
|
16
|
+
Files inside the root directory map directly to URL paths.
|
|
17
|
+
|
|
18
|
+
| File | URL |
|
|
19
|
+
|------|-----|
|
|
20
|
+
| `index.html` | `/` |
|
|
21
|
+
| `about.html` | `/about` or `/about.html` |
|
|
22
|
+
| `api/GET.js` | `GET /api` |
|
|
23
|
+
| `api/POST.js` | `POST /api` |
|
|
24
|
+
| `users/[id]/GET.js` | `GET /users/:id` |
|
|
25
|
+
|
|
26
|
+
Route files checked in order for a directory request: `METHOD.js`, `METHOD.html`, `index.js`, `index.html`.
|
|
27
|
+
|
|
28
|
+
Allowed route file names: `GET.js POST.js PUT.js DELETE.js HEAD.js OPTIONS.js PATCH.js CONNECT.js TRACE.js index.js`
|
|
29
|
+
|
|
30
|
+
## Route Files
|
|
31
|
+
|
|
32
|
+
A route file exports a default async function:
|
|
33
|
+
|
|
34
|
+
```js
|
|
35
|
+
export default async (req, res) => {
|
|
36
|
+
res.json({ id: req.params.id, search: req.query.q });
|
|
37
|
+
};
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Request Object
|
|
41
|
+
|
|
42
|
+
| Property/Method | Description |
|
|
43
|
+
|-----------------|-------------|
|
|
44
|
+
| `req.params` | Dynamic path params from `[param]` segments |
|
|
45
|
+
| `req.query` | Parsed query string as plain object |
|
|
46
|
+
| `req.path` | URL path string |
|
|
47
|
+
| `req.cookies` | Parsed cookies as plain object |
|
|
48
|
+
| `req.body()` | Promise→string of request body |
|
|
49
|
+
| `req.json()` | Promise→parsed JSON body |
|
|
50
|
+
| `req.text()` | Alias for `body()` |
|
|
51
|
+
| `req.buffer()` | Promise→Buffer of request body |
|
|
52
|
+
| `req.get(name)` | Get request header (case-insensitive) |
|
|
53
|
+
| `req.is(type)` | Check Content-Type (e.g. `'json'`) |
|
|
54
|
+
|
|
55
|
+
## Response Object
|
|
56
|
+
|
|
57
|
+
| Method | Description |
|
|
58
|
+
|--------|-------------|
|
|
59
|
+
| `res.status(code)` | Set status code, chainable |
|
|
60
|
+
| `res.set(field, value)` | Set header(s), chainable; accepts object |
|
|
61
|
+
| `res.get(field)` | Get response header |
|
|
62
|
+
| `res.type(t)` | Set Content-Type; shortcuts: html/json/xml/text/css/js |
|
|
63
|
+
| `res.json(obj)` | Send JSON response |
|
|
64
|
+
| `res.send(data)` | Auto-detect: Buffer/object→json, string→text |
|
|
65
|
+
| `res.html(str)` | Send HTML response |
|
|
66
|
+
| `res.text(str)` | Send plain text response |
|
|
67
|
+
| `res.redirect(url, code=302)` | Redirect |
|
|
68
|
+
| `res.cookie(name, val, opts)` | Set cookie; opts: maxAge, domain, path, secure, httpOnly, sameSite |
|
|
69
|
+
| `res.clearCookie(name, opts)` | Clear cookie |
|
|
70
|
+
|
|
71
|
+
## Configuration (.config.json)
|
|
72
|
+
|
|
73
|
+
```json
|
|
74
|
+
{
|
|
75
|
+
"allowedMimes": { ".ext": { "mime": "type/subtype", "encoding": "utf8" } },
|
|
76
|
+
"disallowedRegex": ["pattern"],
|
|
77
|
+
"customRoutes": { "/old": "/new", "/spa/*": "/app.html" },
|
|
78
|
+
"routeFiles": ["GET.js", "index.js"],
|
|
79
|
+
"noRescanPaths": ["pattern"],
|
|
80
|
+
"maxRescanAttempts": 3,
|
|
81
|
+
"middleware": {
|
|
82
|
+
"cors": { "enabled": false, "origin": "*", "methods": "GET,POST", "headers": "*" },
|
|
83
|
+
"compression": { "enabled": false, "threshold": 1024 },
|
|
84
|
+
"rateLimit": { "enabled": false, "windowMs": 60000, "max": 100 },
|
|
85
|
+
"security": { "enabled": true, "headers": { "X-Frame-Options": "DENY" } },
|
|
86
|
+
"logging": { "enabled": true },
|
|
87
|
+
"custom": ["/middleware/auth.js"]
|
|
88
|
+
},
|
|
89
|
+
"cache": {
|
|
90
|
+
"enabled": false,
|
|
91
|
+
"maxSize": 500,
|
|
92
|
+
"maxMemoryMB": 256,
|
|
93
|
+
"ttlMs": 300000,
|
|
94
|
+
"watchFiles": true
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
`customRoutes` values: exact path string (redirect) or `{ "file": "/path.js", "params": {} }` (custom handler file). `*` matches one path segment; `**` matches multiple.
|
|
100
|
+
|
|
101
|
+
## Custom Middleware
|
|
102
|
+
|
|
103
|
+
Middleware files export a default factory:
|
|
104
|
+
|
|
105
|
+
```js
|
|
106
|
+
export default config => async (req, res, next) => {
|
|
107
|
+
// req/res are the enhanced wrappers
|
|
108
|
+
await next();
|
|
109
|
+
};
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## Dynamic Routes
|
|
113
|
+
|
|
114
|
+
Use `[param]` in directory names:
|
|
115
|
+
|
|
116
|
+
```
|
|
117
|
+
users/[id]/GET.js → GET /users/123 (req.params.id === '123')
|
|
118
|
+
posts/[slug]/index.js → /posts/my-title (req.params.slug === 'my-title')
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
## SPA Mode
|
|
122
|
+
|
|
123
|
+
Use `customRoutes` to redirect all page paths to the SPA entry:
|
|
124
|
+
|
|
125
|
+
```json
|
|
126
|
+
"customRoutes": {
|
|
127
|
+
"/": "/app.html",
|
|
128
|
+
"/*.html": "/app.html"
|
|
129
|
+
}
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
## Utility Modules
|
|
133
|
+
|
|
134
|
+
```js
|
|
135
|
+
import { getArgs } from 'kempo-server/utils/cli';
|
|
136
|
+
// getArgs(mapping?) → parsed argv object
|
|
137
|
+
|
|
138
|
+
import { ensureDir, copyDir, emptyDir } from 'kempo-server/utils/fs-utils';
|
|
139
|
+
```
|
|
140
|
+
|
package/package.json
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "kempo-server",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.10.
|
|
4
|
+
"version": "1.10.7",
|
|
5
5
|
"description": "A lightweight, zero-dependency, file based routing server.",
|
|
6
|
-
"main": "dist/index.js",
|
|
7
6
|
"exports": {
|
|
8
|
-
".": "./dist/index.js",
|
|
9
7
|
"./utils/cli": "./dist/utils/cli.js",
|
|
10
8
|
"./utils/fs-utils": "./dist/utils/fs-utils.js"
|
|
11
9
|
},
|