bini-server 2.0.1 → 2.0.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/README.md +193 -223
- package/dist/cli.js +453 -282
- package/dist/cli.js.map +1 -1
- package/dist/index.js +467 -296
- package/dist/index.js.map +1 -1
- package/package.json +6 -4
package/README.md
CHANGED
|
@@ -2,12 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
[](https://www.npmjs.com/package/bini-server)
|
|
4
4
|
[](./LICENSE)
|
|
5
|
-
[](https://nodejs.org)
|
|
6
5
|
[](https://bundlephobia.com/package/bini-server)
|
|
7
6
|
[](https://www.npmjs.com/package/bini-server)
|
|
8
7
|
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
Production server for Bini.js apps.
|
|
9
|
+
It serves the output of your build from `dist/`: static files, pre-rendered pages, the SPA shell, and the compiled API bundle at `dist/api/index.mjs`. Besides Node.js built-in modules, it imports [bini-env](https://www.npmjs.com/package/bini-env) (for `.env` loading) and the `toApiPath` helper from `bini-router/runtime`, so API paths are matched by the same rules as in development.
|
|
11
10
|
|
|
12
11
|
---
|
|
13
12
|
|
|
@@ -19,10 +18,10 @@ Serves the output of `vite build`: pre-rendered pages, static assets, and the co
|
|
|
19
18
|
- [Usage](#usage)
|
|
20
19
|
- [Keyboard Shortcuts](#keyboard-shortcuts)
|
|
21
20
|
- [Environment Variables](#environment-variables)
|
|
22
|
-
- [
|
|
21
|
+
- [Expected Build Output](#expected-build-output)
|
|
23
22
|
- [API Routes](#api-routes)
|
|
24
23
|
- [Static File Serving](#static-file-serving)
|
|
25
|
-
- [Deployment](#deployment)
|
|
24
|
+
- [Deployment Notes](#deployment-notes)
|
|
26
25
|
- [Security](#security)
|
|
27
26
|
- [Verifying the Server](#verifying-the-server)
|
|
28
27
|
- [Configuration Examples](#configuration-examples)
|
|
@@ -36,37 +35,38 @@ Serves the output of `vite build`: pre-rendered pages, static assets, and the co
|
|
|
36
35
|
|
|
37
36
|
### Core
|
|
38
37
|
|
|
39
|
-
- **Static file serving.** Streams `dist/` with MIME types, ETag
|
|
40
|
-
- **API routes.**
|
|
41
|
-
- **
|
|
42
|
-
- **ETag support.** `304 Not Modified`
|
|
43
|
-
- **Sub-path deployments.** Serve the app under a
|
|
38
|
+
- **Static file serving.** Streams files from `dist/` with MIME types, an `ETag`, and `Cache-Control` headers.
|
|
39
|
+
- **API routes.** Forwards `<base>/api` and `<base>/api/...` requests to the `fetch` export of `dist/api/index.mjs`.
|
|
40
|
+
- **Standard page fallbacks.** The same order as `vite preview` and a typical static host: a real file, then a pre-rendered page, then the SPA shell `200.html` (status 200), then `404.html` (status 404), then a plain SPA `index.html`.
|
|
41
|
+
- **ETag support.** Responds `304 Not Modified` when `If-None-Match` matches the file's ETag.
|
|
42
|
+
- **Sub-path deployments.** Serve the app under a base path with `BINI_BASE`.
|
|
43
|
+
- **Optional compression.** Brotli and gzip for text files, enabled with `BINI_COMPRESS`.
|
|
44
44
|
|
|
45
45
|
### Security and Reliability
|
|
46
46
|
|
|
47
47
|
- **Body limits.** Configurable request body size limit for API requests (default 10 MB).
|
|
48
48
|
- **Timeouts.** Configurable body read and handler timeouts (default 30 s each).
|
|
49
|
-
- **Path
|
|
50
|
-
- **Error hiding in production.** With `NODE_ENV=production`,
|
|
51
|
-
- **
|
|
49
|
+
- **Path safety.** Paths with a `..` segment, a backslash, or a null byte are rejected, and `dist/api/` and dotfolders such as `.bini` are never served.
|
|
50
|
+
- **Error hiding in production.** With `NODE_ENV=production`, `500` responses contain only `Internal server error`.
|
|
51
|
+
- **Proxy-aware URLs.** With `BINI_TRUST_PROXY`, the API `Request` URL uses `X-Forwarded-Proto` and `X-Forwarded-Host`.
|
|
52
|
+
- **Health check.** `GET /__health` returns `ok`.
|
|
52
53
|
|
|
53
54
|
### Operations
|
|
54
55
|
|
|
55
|
-
- **
|
|
56
|
-
- **Interactive shortcuts.** `h` for help, `o` to open the browser, `q` to quit.
|
|
57
|
-
- **
|
|
58
|
-
- **
|
|
59
|
-
- **Flexible config.** Supports `BINI_*`, `VITE_*`, or unprefixed environment variables.
|
|
56
|
+
- **Env file loading.** Environment files are loaded through bini-env at startup, and the banner lists the files bini-env reports.
|
|
57
|
+
- **Interactive shortcuts.** `h` for help, `o` to open the browser, `q` to quit (only when stdin is a terminal).
|
|
58
|
+
- **Graceful shutdown.** Handles `SIGTERM` and `SIGINT`, with a 10 second fallback.
|
|
59
|
+
- **Flexible config.** Most settings accept `BINI_*`, `VITE_*`, or unprefixed environment variables.
|
|
60
60
|
|
|
61
61
|
---
|
|
62
62
|
|
|
63
63
|
## Requirements
|
|
64
64
|
|
|
65
|
-
- Node.js
|
|
66
|
-
- A
|
|
67
|
-
- `dist/` (required; the server exits if it is missing)
|
|
68
|
-
- `dist/api/index.mjs` (
|
|
69
|
-
-
|
|
65
|
+
- A recent Node.js. The server uses the global `Request` and `Response` classes, `Readable.fromWeb`, and `server.closeAllConnections()`.
|
|
66
|
+
- A finished build in `dist/` (relative to the working directory):
|
|
67
|
+
- `dist/` (required; the server exits with code 1 if it is missing)
|
|
68
|
+
- `dist/api/index.mjs` (the compiled API bundle; it must export a function named `fetch`)
|
|
69
|
+
- Pre-rendered pages, `200.html`, `404.html`, and `index.html` (whichever your build produces)
|
|
70
70
|
|
|
71
71
|
---
|
|
72
72
|
|
|
@@ -106,15 +106,18 @@ npm start # Serve in production
|
|
|
106
106
|
|
|
107
107
|
```text
|
|
108
108
|
ß Bini.js (production)
|
|
109
|
-
➜ Environments: .env
|
|
109
|
+
➜ Environments: .env
|
|
110
110
|
➜ Local: http://localhost:3000/
|
|
111
111
|
➜ Network: http://192.168.1.5:3000/
|
|
112
112
|
➜ press h + enter to show help
|
|
113
113
|
```
|
|
114
114
|
|
|
115
|
-
- `
|
|
116
|
-
- `
|
|
117
|
-
- `
|
|
115
|
+
- `(production)` is always printed.
|
|
116
|
+
- `Environments` lists the env files reported by bini-env, relative to the working directory. It is omitted when there are none.
|
|
117
|
+
- `Local` and `Network` show the port that was actually bound and include the base path when one is set (for example `http://localhost:3000/app/`).
|
|
118
|
+
- `Local` uses `localhost` when the server listens on `0.0.0.0`, `::`, or a loopback address, and the configured host otherwise.
|
|
119
|
+
- `Network` appears only when the server listens on `0.0.0.0` or `::`. It is the first private IPv4 address (`10.x.x.x`, `172.16-31.x.x`, `192.168.x.x`) of a non-internal interface. If there is none, it falls back to the first other non-internal IPv4 address (link-local `169.254.x.x` addresses are skipped). Interfaces whose names start with `veth`, `br-`, `utun`, `tun`, or `tap`, or contain `docker`, `vmnet`, or `vbox` (case-insensitive), are ignored. The line is omitted when no address is found.
|
|
120
|
+
- `Base` appears as an extra line when a base path is set.
|
|
118
121
|
|
|
119
122
|
### Programmatic start
|
|
120
123
|
|
|
@@ -128,12 +131,14 @@ await start()
|
|
|
128
131
|
|
|
129
132
|
### What `start()` does
|
|
130
133
|
|
|
131
|
-
1. Loads
|
|
132
|
-
2.
|
|
133
|
-
3.
|
|
134
|
-
4.
|
|
135
|
-
5.
|
|
136
|
-
6.
|
|
134
|
+
1. Loads env files with bini-env (mode is `NODE_ENV`, or `production` when it is not set), then re-reads the configuration.
|
|
135
|
+
2. Prints a warning if the base path came from the unprefixed `BASE` variable (see [Environment Variables](#environment-variables)).
|
|
136
|
+
3. Prints an error and exits with code 1 when the `dist` directory does not exist.
|
|
137
|
+
4. Chooses the port (see [Port selection](#port-selection)).
|
|
138
|
+
5. Creates the HTTP server, starts the keyboard shortcuts (if stdin is a terminal), listens, and prints the banner once listening.
|
|
139
|
+
6. Registers `SIGTERM`, `SIGINT`, `unhandledRejection`, and `uncaughtException` handlers.
|
|
140
|
+
|
|
141
|
+
`dist/api/index.mjs` is not loaded at startup. It is loaded on the first API request (see [API Routes](#api-routes)).
|
|
137
142
|
|
|
138
143
|
---
|
|
139
144
|
|
|
@@ -147,7 +152,7 @@ While the server is running, type a key and press `enter`:
|
|
|
147
152
|
| `o` | Open `http://localhost:<port><base>/` in the default browser |
|
|
148
153
|
| `q` | Quit the server |
|
|
149
154
|
|
|
150
|
-
> Keyboard shortcuts are
|
|
155
|
+
> Keyboard shortcuts are only enabled when stdin is a terminal. While they are active, closing stdin exits the process.
|
|
151
156
|
|
|
152
157
|
---
|
|
153
158
|
|
|
@@ -155,18 +160,11 @@ While the server is running, type a key and press `enter`:
|
|
|
155
160
|
|
|
156
161
|
### `.env` Files
|
|
157
162
|
|
|
158
|
-
At startup, bini-server
|
|
159
|
-
|
|
160
|
-
1. `.env.[mode].local`
|
|
161
|
-
2. `.env.[mode]`
|
|
162
|
-
3. `.env.local`
|
|
163
|
-
4. `.env`
|
|
164
|
-
|
|
165
|
-
A variable that is already set in the environment is never overridden by a file, and a higher-priority file wins over a lower one. The loaded values are available through `process.env`, which is also what API routes receive as `env`. The server settings below can come from these files too.
|
|
163
|
+
At startup, bini-server calls bini-env's `loadEnvFiles` for the working directory, using `NODE_ENV` as the mode (or `production` when it is not set), and then re-reads its own configuration. Which files are loaded and in what priority is defined by bini-env; see its documentation.
|
|
166
164
|
|
|
167
165
|
### Server Configuration
|
|
168
166
|
|
|
169
|
-
|
|
167
|
+
These settings support three naming conventions:
|
|
170
168
|
|
|
171
169
|
| Convention | Example | Priority |
|
|
172
170
|
| --- | --- | --- |
|
|
@@ -176,74 +174,74 @@ All settings support **three naming conventions:**
|
|
|
176
174
|
|
|
177
175
|
| Variable | Default | Description |
|
|
178
176
|
| --- | --- | --- |
|
|
179
|
-
| `PORT` | `3000` |
|
|
177
|
+
| `PORT` | `3000` | Port to listen on (see [Port selection](#port-selection)) |
|
|
180
178
|
| `DIST_DIR` | `dist` | Build output folder, relative to the working directory |
|
|
181
|
-
| `BASE` | *(empty)* | Sub-path the app is served under. `''`, `/`, `.`, and `./` mean no base; any other value is normalized to a leading slash with no trailing slash (`app/` becomes `/app`) |
|
|
182
179
|
| `BODY_TIMEOUT_SECS` | `30` | Max seconds to read an API request body |
|
|
183
180
|
| `HANDLER_TIMEOUT_SECS` | `30` | Max seconds for the API handler to respond |
|
|
184
181
|
| `BODY_SIZE_LIMIT` | `10485760` | Max API request body size in bytes (10 MB) |
|
|
185
182
|
|
|
186
|
-
|
|
183
|
+
Numeric values are parsed with `parseInt`; a value that cannot be parsed as an integer falls back to the default. Setting a limit or timeout to `0` does not disable it: a body limit of `0` rejects any non-empty request body.
|
|
184
|
+
|
|
185
|
+
These settings are read in a fixed form:
|
|
186
|
+
|
|
187
|
+
| Variable | Default | Description |
|
|
188
|
+
| --- | --- | --- |
|
|
189
|
+
| `BINI_BASE` (or `VITE_BASE`) | *(empty)* | Sub-path the app is served under. `''`, `/`, `.`, and `./` mean no base; any other value is normalized to a leading slash with no trailing slash (`app/` becomes `/app`). The unprefixed `BASE` is still read as a fallback, with a warning, because other tools also set it |
|
|
190
|
+
| `BINI_HOST` | `0.0.0.0` | Address to listen on. Only the `BINI_` name is read, because bare names like `HOST` are set by other tools |
|
|
191
|
+
| `BINI_TRUST_PROXY` | off | Trust `X-Forwarded-Proto` and `X-Forwarded-Host` when building the API request URL |
|
|
192
|
+
| `BINI_COMPRESS` | off | Compress text responses with Brotli or gzip |
|
|
193
|
+
|
|
194
|
+
`BINI_TRUST_PROXY` and `BINI_COMPRESS` are on when set to `1`, `true`, `yes`, or `on` (case-insensitive).
|
|
187
195
|
|
|
188
|
-
The server also reads `NODE_ENV`.
|
|
196
|
+
The server also reads `NODE_ENV`. Only when it is exactly `production` do `500` responses hide the underlying error message; otherwise the message is included.
|
|
189
197
|
|
|
190
198
|
### Port selection
|
|
191
199
|
|
|
192
|
-
|
|
200
|
+
- **`PORT` is set** (in any of the three forms): the server uses exactly that port. A value that is not an integer from 1 to 65535 stops the server with `invalid port`, and a port that is already in use stops it with `port <n> is already in use on <host>`. Both exit with code 1.
|
|
201
|
+
- **`PORT` is not set:** the server starts at `3000` and tries the next port while one is busy. It logs one line for every busy port it skips:
|
|
193
202
|
|
|
194
203
|
```text
|
|
195
204
|
Port 3000 is in use, trying another one...
|
|
196
205
|
```
|
|
197
206
|
|
|
198
|
-
The
|
|
207
|
+
The search covers 100 ports (`3000` through `3099`) on the configured host; if none is free, `start()` rejects with `No free port found in range <start>–<end>`. The banner shows the port that was actually used.
|
|
199
208
|
|
|
200
209
|
### Examples
|
|
201
210
|
|
|
202
211
|
```bash
|
|
203
|
-
#
|
|
204
|
-
PORT=8080
|
|
205
|
-
BODY_SIZE_LIMIT=5242880 # 5MB
|
|
206
|
-
|
|
207
|
-
# Or inline
|
|
212
|
+
# Inline
|
|
208
213
|
PORT=3001 bini-server
|
|
209
214
|
|
|
210
|
-
#
|
|
215
|
+
# With the BINI prefix (takes precedence over PORT)
|
|
211
216
|
BINI_PORT=3002 bini-server
|
|
217
|
+
|
|
218
|
+
# Only reachable from this machine
|
|
219
|
+
BINI_HOST=127.0.0.1 bini-server
|
|
212
220
|
```
|
|
213
221
|
|
|
214
222
|
---
|
|
215
223
|
|
|
216
|
-
##
|
|
224
|
+
## Expected Build Output
|
|
225
|
+
|
|
226
|
+
bini-server serves whatever is in `dist/` (or `DIST_DIR`). It does not read your source files at runtime.
|
|
217
227
|
|
|
218
228
|
```text
|
|
219
|
-
|
|
220
|
-
├──
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
├── src/
|
|
229
|
-
│ ├── app/
|
|
230
|
-
│ │ ├── api/ # API handlers, compiled into dist/api at build time
|
|
231
|
-
│ │ │ ├── users.ts
|
|
232
|
-
│ │ │ └── posts/
|
|
233
|
-
│ │ │ ├── index.ts
|
|
234
|
-
│ │ │ └── [id].ts
|
|
235
|
-
│ │ └── layout.tsx
|
|
236
|
-
│ └── main.tsx
|
|
237
|
-
├── .env # Environment variables
|
|
238
|
-
├── package.json
|
|
239
|
-
└── vite.config.ts
|
|
229
|
+
dist/
|
|
230
|
+
├── index.html
|
|
231
|
+
├── about/index.html # Pre-rendered page
|
|
232
|
+
├── about.html # Same page, file form
|
|
233
|
+
├── 200.html # SPA shell (bini-ssg), used for every other page URL
|
|
234
|
+
├── 404.html
|
|
235
|
+
├── assets/
|
|
236
|
+
└── api/
|
|
237
|
+
└── index.mjs # API bundle (never served as a file)
|
|
240
238
|
```
|
|
241
239
|
|
|
242
240
|
---
|
|
243
241
|
|
|
244
242
|
## API Routes
|
|
245
243
|
|
|
246
|
-
Requests to `<base>/api` and `<base>/api/...` are passed to the `fetch` export of `dist/api/index.mjs
|
|
244
|
+
Requests to `<base>/api` and `<base>/api/...` are passed to the `fetch` export of `dist/api/index.mjs`. The bundle is imported once, lazily, when the first API request arrives, and the result is reused for all later requests. API requests are handled before the method check, so every HTTP method reaches the bundle.
|
|
247
245
|
|
|
248
246
|
### What the bundle receives
|
|
249
247
|
|
|
@@ -251,63 +249,33 @@ Requests to `<base>/api` and `<base>/api/...` are passed to the `fetch` export o
|
|
|
251
249
|
fetch(request: Request, env: NodeJS.ProcessEnv, ctx: { waitUntil(p: Promise<unknown>): void })
|
|
252
250
|
```
|
|
253
251
|
|
|
254
|
-
-
|
|
255
|
-
-
|
|
256
|
-
-
|
|
252
|
+
- `request`: a standard `Request` whose URL is `<proto>://<host><original request URL>`. The protocol is `http` and the host is the `Host` header (`localhost` when there is none). With `BINI_TRUST_PROXY`, the first value of `X-Forwarded-Proto` (`http` or `https`) and `X-Forwarded-Host` (letters, digits, `.`, `-`, `:`, `[`, `]` only) are used instead. The URL keeps the original path, including the base and `/api`. Header arrays are joined with `,` (the `cookie` header with `;`). The body is read for every method except `GET` and `HEAD`.
|
|
253
|
+
- `env`: `process.env`.
|
|
254
|
+
- `ctx.waitUntil`: accepts a promise and swallows its rejection; the server does not wait for it.
|
|
257
255
|
|
|
258
256
|
The response status and headers (including every `Set-Cookie`) are copied, and the body is streamed to the client. `HEAD` requests receive headers only.
|
|
259
257
|
|
|
260
|
-
### Supported handler formats
|
|
261
|
-
|
|
262
|
-
How handlers are written, routed, and matched is defined by bini-router. For example:
|
|
263
|
-
|
|
264
|
-
```typescript
|
|
265
|
-
// src/app/api/hello.ts
|
|
266
|
-
|
|
267
|
-
// 1. Hono app (default export with .fetch)
|
|
268
|
-
import { Hono } from 'hono'
|
|
269
|
-
const app = new Hono()
|
|
270
|
-
app.get('/hello', (c) => c.json({ message: 'Hello' }))
|
|
271
|
-
export default app
|
|
272
|
-
|
|
273
|
-
// 2. Plain function (default export)
|
|
274
|
-
export default (req: Request) => Response.json({ message: 'Hello' })
|
|
275
|
-
|
|
276
|
-
// 3. Per-method exports
|
|
277
|
-
export function GET(req: Request, { params }) {
|
|
278
|
-
return Response.json({ id: params.id })
|
|
279
|
-
}
|
|
280
|
-
```
|
|
281
|
-
|
|
282
|
-
Only `.ts` and `.js` files in `src/app/api/` are used.
|
|
283
|
-
|
|
284
|
-
### Dynamic routes
|
|
285
|
-
|
|
286
|
-
```text
|
|
287
|
-
src/app/api/
|
|
288
|
-
users/
|
|
289
|
-
[id].ts → /api/users/:id
|
|
290
|
-
posts/
|
|
291
|
-
[...slug].ts → /api/posts/*
|
|
292
|
-
```
|
|
293
|
-
|
|
294
|
-
Route parameters arrive in the second argument of a handler (`{ params, searchParams, env, waitUntil }`). See the [bini-router](https://www.npmjs.com/package/bini-router) documentation for the full routing rules.
|
|
295
|
-
|
|
296
258
|
### CORS
|
|
297
259
|
|
|
298
|
-
bini-server does not add CORS headers or answer preflight requests
|
|
260
|
+
bini-server does not add CORS headers or answer preflight requests. Any CORS handling has to come from the API bundle.
|
|
299
261
|
|
|
300
262
|
### Base path
|
|
301
263
|
|
|
302
|
-
|
|
264
|
+
When a base path is set, only requests under it are treated as API requests (`<base>/api/...`). The `Request` passed to the bundle keeps the full original URL, including the base.
|
|
303
265
|
|
|
304
266
|
### Request limits
|
|
305
267
|
|
|
306
|
-
|
|
268
|
+
`BODY_SIZE_LIMIT` and `BODY_TIMEOUT_SECS` apply while the body is read, and `HANDLER_TIMEOUT_SECS` applies while waiting for the handler. The handler is not cancelled when the timeout fires. A request whose `Content-Length` is already above the limit is rejected with `413` before the body is read.
|
|
269
|
+
|
|
270
|
+
### When the bundle is missing or broken
|
|
307
271
|
|
|
308
|
-
|
|
272
|
+
If `dist/api/index.mjs` does not exist, fails to import, or does not export a `fetch` function, the server prints a `[WARN] failed to load handler for <path>:` message with the underlying error, and the request returns `503` with a body like this:
|
|
273
|
+
|
|
274
|
+
```json
|
|
275
|
+
{ "error": "failed to load handler", "path": "/api/hello", "detail": "<error message>" }
|
|
276
|
+
```
|
|
309
277
|
|
|
310
|
-
|
|
278
|
+
The load is attempted again on the next API request, so a bundle that appears later (or is fixed) is picked up without a restart.
|
|
311
279
|
|
|
312
280
|
---
|
|
313
281
|
|
|
@@ -315,113 +283,91 @@ If `dist/api/index.mjs` is missing or does not export `fetch()`, the server prin
|
|
|
315
283
|
|
|
316
284
|
### Lookup order
|
|
317
285
|
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
For a path **without** an extension, the server uses the first of these that applies:
|
|
286
|
+
A request is served from `dist/` in this order. The first step that applies wins:
|
|
321
287
|
|
|
322
288
|
| Order | Looks for | Status |
|
|
323
289
|
| --- | --- | --- |
|
|
324
|
-
| 1 |
|
|
325
|
-
| 2 |
|
|
326
|
-
| 3 | `
|
|
327
|
-
| 4 | `dist/
|
|
328
|
-
| 5 |
|
|
290
|
+
| 1 | The file itself, or `<path>/index.html` when the path is a folder (a pre-rendered page) | 200 |
|
|
291
|
+
| 2 | `<path>.html`, for paths without an extension (the file form of a pre-rendered page) | 200 |
|
|
292
|
+
| 3 | Nothing, when the path ends in a known asset extension (`.js`, `.css`, `.png`, ...): plain-text `Not found` | 404 |
|
|
293
|
+
| 4 | `dist/200.html`, the SPA shell. Your client router renders the matching page or its not-found page | 200 |
|
|
294
|
+
| 5 | `dist/404.html`, when there is no `200.html` | 404 |
|
|
295
|
+
| 6 | `dist/index.html`, for a plain SPA build without `bini-ssg` | 200 |
|
|
296
|
+
| 7 | Nothing found: plain-text `Not found` | 404 |
|
|
329
297
|
|
|
330
|
-
|
|
298
|
+
Known asset extensions: `.js`, `.mjs`, `.css`, `.map`, `.json`, `.txt`, `.xml`, `.webmanifest`, `.png`, `.jpg`, `.jpeg`, `.gif`, `.svg`, `.ico`, `.webp`, `.avif`, `.woff`, `.woff2`, `.ttf`, `.otf`, `.eot`, `.pdf`, `.wasm`, `.mp4`, `.webm`, `.mp3`. Any other dotted path, such as `/blog/v1.2`, is treated as a page URL.
|
|
331
299
|
|
|
332
|
-
|
|
300
|
+
This means a dynamic route that was not pre-rendered (for example a `/blog/<slug>` that is not in your static params) gets the `200.html` shell and is rendered by the client.
|
|
333
301
|
|
|
334
|
-
|
|
302
|
+
When a base path is set, only paths under it are served; other paths get the plain-text `404 Not found`.
|
|
335
303
|
|
|
336
|
-
|
|
337
|
-
| --- | --- |
|
|
338
|
-
| `blog/[slug]` | `/blog/anything` (exactly one segment in that position) |
|
|
339
|
-
| `docs/[...path]` | `/docs/a`, `/docs/a/b` (at least one segment) |
|
|
340
|
-
| `docs/[[...path]]` | `/docs`, `/docs/a`, `/docs/a/b` (zero or more segments) |
|
|
304
|
+
### Paths that are never served
|
|
341
305
|
|
|
342
|
-
|
|
306
|
+
- `dist/api/` (the compiled API code) and anything else whose first segment is `api`.
|
|
307
|
+
- Any path with a segment that starts with `.` (such as `.bini`), except `.well-known`. These return `404`.
|
|
308
|
+
- Paths with a `..` segment, a backslash, or a null byte, or that resolve outside `dist/`, and URLs with invalid percent-encoding, return `400 Bad request`.
|
|
343
309
|
|
|
344
310
|
### Supported MIME Types
|
|
345
311
|
|
|
346
|
-
|
|
|
312
|
+
| Extensions | Content-Type |
|
|
347
313
|
| --- | --- |
|
|
348
|
-
|
|
|
349
|
-
|
|
|
350
|
-
|
|
|
351
|
-
|
|
352
|
-
|
|
314
|
+
| `.html` | `text/html; charset=utf-8` |
|
|
315
|
+
| `.js`, `.mjs` | `text/javascript; charset=utf-8` |
|
|
316
|
+
| `.css` | `text/css; charset=utf-8` |
|
|
317
|
+
| `.json`, `.map` | `application/json; charset=utf-8` |
|
|
318
|
+
| `.png` | `image/png` |
|
|
319
|
+
| `.jpg`, `.jpeg` | `image/jpeg` |
|
|
320
|
+
| `.gif` | `image/gif` |
|
|
321
|
+
| `.svg` | `image/svg+xml; charset=utf-8` |
|
|
322
|
+
| `.ico` | `image/x-icon` |
|
|
323
|
+
| `.webp` | `image/webp` |
|
|
324
|
+
| `.avif` | `image/avif` |
|
|
325
|
+
| `.woff` | `font/woff` |
|
|
326
|
+
| `.woff2` | `font/woff2` |
|
|
327
|
+
| `.ttf` | `font/ttf` |
|
|
328
|
+
| `.otf` | `font/otf` |
|
|
329
|
+
| `.eot` | `application/vnd.ms-fontobject` |
|
|
330
|
+
| `.txt` | `text/plain; charset=utf-8` |
|
|
331
|
+
| `.xml` | `application/xml; charset=utf-8` |
|
|
332
|
+
| `.webmanifest` | `application/manifest+json; charset=utf-8` |
|
|
333
|
+
| `.pdf` | `application/pdf` |
|
|
334
|
+
| `.wasm` | `application/wasm` |
|
|
335
|
+
| `.mp4` | `video/mp4` |
|
|
336
|
+
| `.webm` | `video/webm` |
|
|
337
|
+
| `.mp3` | `audio/mpeg` |
|
|
338
|
+
|
|
339
|
+
Any other extension is served as `application/octet-stream`. Every file response also carries `ETag`, `Cache-Control`, and `X-Content-Type-Options: nosniff`, plus `Content-Length` when the response is not compressed.
|
|
353
340
|
|
|
354
341
|
### Cache Headers
|
|
355
342
|
|
|
356
|
-
| File
|
|
343
|
+
| File | Cache policy |
|
|
357
344
|
| --- | --- |
|
|
358
|
-
|
|
|
359
|
-
|
|
|
345
|
+
| A hashed file under `assets/` | `public, max-age=31536000, immutable` (1 year) |
|
|
346
|
+
| Everything else | `no-cache` |
|
|
347
|
+
|
|
348
|
+
A file counts as hashed when it is inside `dist/assets/` and its name ends with a separator (`-` or `.`) followed by 8 characters from `A-Z`, `a-z`, `0-9`, `_`, and `-`, at least one of them a digit (for example `assets/index-Bx3k9a2q.js`). Other files in `assets/` get `no-cache`.
|
|
360
349
|
|
|
361
350
|
### ETag Support
|
|
362
351
|
|
|
363
|
-
|
|
352
|
+
- An `ETag` header is sent on every file response. It is the first 16 characters of an MD5 hex digest of the file's size and modified time, in quotes. For compressed responses, `-br` or `-gzip` is added inside the quotes.
|
|
353
|
+
- A request whose `If-None-Match` matches the ETag (a list of tags, weak `W/` tags, and `*` are understood) receives `304 Not Modified`. This check is skipped when the response is `404.html`, so a missing URL is never reported as unchanged.
|
|
364
354
|
|
|
365
|
-
|
|
366
|
-
- A request whose `If-None-Match` equals the ETag exactly receives `304 Not Modified` (for `200` responses).
|
|
355
|
+
### Compression
|
|
367
356
|
|
|
368
|
-
|
|
357
|
+
Compression is off by default. With `BINI_COMPRESS` on, `.html`, `.js`, `.mjs`, `.css`, `.json`, `.svg`, `.txt`, `.xml`, `.webmanifest`, and `.map` files of at least 1 KB are compressed with Brotli (quality 4) or gzip, whichever the client accepts, preferring Brotli. Those responses carry `Vary: Accept-Encoding` and `Content-Encoding`, and no `Content-Length`.
|
|
369
358
|
|
|
370
359
|
---
|
|
371
360
|
|
|
372
|
-
## Deployment
|
|
373
|
-
|
|
374
|
-
### What to ship
|
|
375
|
-
|
|
376
|
-
bini-server needs the build output, not your source: deploy `dist/` (including `dist/api/index.mjs`), `package.json`, and the installed dependencies. The compiled API leaves packages from `node_modules` external, so dependencies must be installed on the server.
|
|
377
|
-
|
|
378
|
-
- **VPS / pm2:** deploy the project directory, install dependencies, and build
|
|
379
|
-
- **Railway, Render, Fly.io:** build as part of the deploy
|
|
380
|
-
- **Docker:** build inside the image, as shown below
|
|
381
|
-
|
|
382
|
-
### VPS / Dedicated Server
|
|
383
|
-
|
|
384
|
-
```bash
|
|
385
|
-
npm run build
|
|
386
|
-
npm start
|
|
387
|
-
|
|
388
|
-
# With pm2 (recommended)
|
|
389
|
-
npm install -g pm2
|
|
390
|
-
pm2 start "npm start" --name my-app
|
|
391
|
-
pm2 save
|
|
392
|
-
pm2 startup
|
|
393
|
-
```
|
|
394
|
-
|
|
395
|
-
### Platform as a Service
|
|
396
|
-
|
|
397
|
-
| Platform | Start Command | Notes |
|
|
398
|
-
| --- | --- | --- |
|
|
399
|
-
| **Railway** | `npm start` | PORT injected automatically |
|
|
400
|
-
| **Render** | `npm start` | PORT injected automatically |
|
|
401
|
-
| **Fly.io** | `npm start` | See the fly.toml example below |
|
|
402
|
-
| **Heroku** | `npm start` | PORT injected automatically |
|
|
403
|
-
|
|
404
|
-
### Docker
|
|
405
|
-
|
|
406
|
-
```dockerfile
|
|
407
|
-
FROM node:20-alpine
|
|
408
|
-
WORKDIR /app
|
|
409
|
-
COPY package*.json ./
|
|
410
|
-
RUN npm ci
|
|
411
|
-
COPY . .
|
|
412
|
-
RUN npm run build && npm prune --omit=dev
|
|
413
|
-
ENV NODE_ENV=production
|
|
414
|
-
EXPOSE 3000
|
|
415
|
-
CMD ["npm", "start"]
|
|
416
|
-
```
|
|
417
|
-
|
|
418
|
-
### Fly.io
|
|
361
|
+
## Deployment Notes
|
|
419
362
|
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
363
|
+
- The server resolves `dist` (or `DIST_DIR`) against the process's current working directory, so start it from your project root.
|
|
364
|
+
- It listens on `0.0.0.0` unless `BINI_HOST` says otherwise.
|
|
365
|
+
- Set `PORT` (or `BINI_PORT` / `VITE_PORT`) to choose the port. When it is set, the server does not look for another port.
|
|
366
|
+
- Behind a reverse proxy, set `BINI_TRUST_PROXY=1` if your API code needs the original protocol and host in `request.url`.
|
|
367
|
+
- On `SIGTERM` or `SIGINT` it stops accepting connections, closes idle connections, and exits with code 0 once the server has closed. After 10 seconds it closes the remaining connections and exits with code 1. A second signal exits immediately with code 1.
|
|
368
|
+
- An unhandled promise rejection or uncaught exception is logged and exits with code 1.
|
|
369
|
+
- Keyboard shortcuts are disabled when stdin is not a terminal.
|
|
370
|
+
- Fixed (not configurable) server settings: keep-alive timeout 65 s, headers timeout 66 s, and max 2000 headers.
|
|
425
371
|
|
|
426
372
|
---
|
|
427
373
|
|
|
@@ -432,10 +378,12 @@ CMD ["npm", "start"]
|
|
|
432
378
|
| Body size limit (API) | 10 MB | `BODY_SIZE_LIMIT` |
|
|
433
379
|
| Body read timeout (API) | 30 s | `BODY_TIMEOUT_SECS` |
|
|
434
380
|
| Handler timeout (API) | 30 s | `HANDLER_TIMEOUT_SECS` |
|
|
435
|
-
| Path traversal | `..` segments and null bytes are
|
|
436
|
-
| Compiled API files | `dist/api/`
|
|
437
|
-
|
|
|
438
|
-
|
|
|
381
|
+
| Path traversal | `..` segments, backslashes, and null bytes are rejected with `400` | Not configurable |
|
|
382
|
+
| Compiled API files and dotfolders | `dist/api/` and dot-prefixed paths (except `.well-known`) are never served | Not configurable |
|
|
383
|
+
| Static methods | Only `GET` and `HEAD`; others get `405` | Not configurable |
|
|
384
|
+
| Forwarded headers | Ignored | `BINI_TRUST_PROXY` |
|
|
385
|
+
| Error messages | `500` responses hide the message when `NODE_ENV=production` | `NODE_ENV` |
|
|
386
|
+
| CORS | Not handled by bini-server | n/a |
|
|
439
387
|
|
|
440
388
|
---
|
|
441
389
|
|
|
@@ -445,10 +393,19 @@ CMD ["npm", "start"]
|
|
|
445
393
|
# Check static files
|
|
446
394
|
curl http://localhost:3000/
|
|
447
395
|
|
|
448
|
-
# Check
|
|
396
|
+
# Check the health endpoint
|
|
397
|
+
curl http://localhost:3000/__health
|
|
398
|
+
|
|
399
|
+
# Check an API route (if your API bundle handles it)
|
|
449
400
|
curl http://localhost:3000/api/hello
|
|
450
401
|
|
|
451
|
-
# Check
|
|
402
|
+
# Check an unknown page URL: 200 with the SPA shell (404 only without 200.html)
|
|
403
|
+
curl -I http://localhost:3000/not-a-real-page
|
|
404
|
+
|
|
405
|
+
# Check a missing file: 404
|
|
406
|
+
curl -I http://localhost:3000/missing.js
|
|
407
|
+
|
|
408
|
+
# Check ETag (use a file that exists in dist/)
|
|
452
409
|
curl -I http://localhost:3000/assets/app.js
|
|
453
410
|
|
|
454
411
|
# Test 304 Not Modified
|
|
@@ -473,7 +430,14 @@ BODY_SIZE_LIMIT=10485760
|
|
|
473
430
|
### Served under a sub-path
|
|
474
431
|
|
|
475
432
|
```bash
|
|
476
|
-
|
|
433
|
+
BINI_BASE=/app
|
|
434
|
+
```
|
|
435
|
+
|
|
436
|
+
### Behind a reverse proxy with compression
|
|
437
|
+
|
|
438
|
+
```bash
|
|
439
|
+
BINI_TRUST_PROXY=1
|
|
440
|
+
BINI_COMPRESS=1
|
|
477
441
|
```
|
|
478
442
|
|
|
479
443
|
### Smaller request bodies
|
|
@@ -489,37 +453,43 @@ BODY_SIZE_LIMIT=104857600 # 100MB
|
|
|
489
453
|
BODY_TIMEOUT_SECS=300 # 5 minutes
|
|
490
454
|
```
|
|
491
455
|
|
|
492
|
-
Raise `bodySizeLimit` in `biniroute()` too, otherwise the bundle's own limit still applies.
|
|
493
|
-
|
|
494
456
|
---
|
|
495
457
|
|
|
496
458
|
## API Reference
|
|
497
459
|
|
|
498
460
|
### Environment Variables Priority
|
|
499
461
|
|
|
462
|
+
For `PORT`, `DIST_DIR`, `BODY_TIMEOUT_SECS`, `HANDLER_TIMEOUT_SECS`, and `BODY_SIZE_LIMIT`:
|
|
463
|
+
|
|
500
464
|
1. `BINI_*` (highest)
|
|
501
465
|
2. `VITE_*` (medium)
|
|
502
466
|
3. No prefix (lowest)
|
|
503
467
|
|
|
504
|
-
|
|
468
|
+
`BASE` follows `BINI_BASE`, then `VITE_BASE`, then the unprefixed `BASE`. `BINI_HOST`, `BINI_TRUST_PROXY`, and `BINI_COMPRESS` are read only with the `BINI_` prefix.
|
|
469
|
+
|
|
470
|
+
### HTTP Status Codes Produced by bini-server
|
|
505
471
|
|
|
506
|
-
|
|
472
|
+
API handlers may return any status.
|
|
507
473
|
|
|
508
474
|
| Code | Description |
|
|
509
475
|
| --- | --- |
|
|
510
|
-
| `200` | Success |
|
|
476
|
+
| `200` | Success, including the `200.html` shell for page URLs and `/__health` |
|
|
511
477
|
| `304` | Not Modified (ETag match) |
|
|
512
|
-
| `400` | Bad request URL
|
|
513
|
-
| `404` |
|
|
514
|
-
| `
|
|
515
|
-
| `
|
|
516
|
-
| `
|
|
478
|
+
| `400` | Bad request: unsafe or malformed path, invalid percent-encoding, bad API request URL, or an error while reading the API request body |
|
|
479
|
+
| `404` | Missing file, denied path, or unknown path (plain-text `Not found`), or `404.html` for a page URL when there is no `200.html` |
|
|
480
|
+
| `405` | A non-API request with a method other than `GET` or `HEAD` (`Allow: GET, HEAD`) |
|
|
481
|
+
| `408` | API request body not received within `BODY_TIMEOUT_SECS` |
|
|
482
|
+
| `413` | API request body larger than `BODY_SIZE_LIMIT` |
|
|
483
|
+
| `500` | API handler error or handler timeout, or an error in a request middleware |
|
|
484
|
+
| `503` | The API bundle could not be loaded (`failed to load handler`) |
|
|
485
|
+
|
|
486
|
+
The `408` and `413` responses are sent with `Connection: close`.
|
|
517
487
|
|
|
518
|
-
|
|
488
|
+
With `NODE_ENV=production` the message in `500` responses from handler errors is always `Internal server error`.
|
|
519
489
|
|
|
520
490
|
### Supported HTTP Methods
|
|
521
491
|
|
|
522
|
-
|
|
492
|
+
API requests accept every method; the bundle decides which it handles. All other requests accept only `GET` and `HEAD`, and `HEAD` receives headers only.
|
|
523
493
|
|
|
524
494
|
---
|
|
525
495
|
|