ultimate-express 1.2.13 → 1.2.14
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/EXPRESS_LICENSE +25 -25
- package/LICENSE +201 -201
- package/README.md +312 -312
- package/package.json +86 -86
- package/src/application.js +302 -302
- package/src/index.js +37 -37
- package/src/middlewares.js +316 -316
- package/src/request.js +402 -379
- package/src/response.js +762 -762
- package/src/router.js +541 -541
- package/src/utils.js +338 -338
- package/src/view.js +118 -118
- package/src/worker.js +30 -30
package/README.md
CHANGED
|
@@ -1,313 +1,313 @@
|
|
|
1
|
-
# µExpress / Ultimate Express
|
|
2
|
-
|
|
3
|
-
The *Ultimate* Express. Fastest http server with **full** Express compatibility, based on µWebSockets.
|
|
4
|
-
|
|
5
|
-
This library is a very fast re-implementation of Express.js 4.
|
|
6
|
-
It is designed to be a drop-in replacement for Express.js, with the same API and functionality, while being much faster. It is not a fork of Express.js.
|
|
7
|
-
To make sure µExpress matches behavior of Express in all cases, we run all tests with Express first, and then with µExpress and compare results to make sure they match.
|
|
8
|
-
|
|
9
|
-
`npm install ultimate-express` -> replace `express` with `ultimate-express` -> done[*](https://github.com/dimdenGD/ultimate-express?tab=readme-ov-file#differences-from-express)
|
|
10
|
-
|
|
11
|
-
[](https://nodejs.org)
|
|
12
|
-
[](https://npmjs.com/package/ultimate-express)
|
|
13
|
-
[](https://patreon.com/dimdendev)
|
|
14
|
-
|
|
15
|
-
## Difference from similar projects
|
|
16
|
-
|
|
17
|
-
Similar projects based on uWebSockets:
|
|
18
|
-
|
|
19
|
-
- `express` on Bun - since Bun uses uWS for its HTTP module, Express is about 2-3 times faster than on Node.js, but still slower than µExpress because it doesn't do uWS-specific optimizations.
|
|
20
|
-
- `hyper-express` - while having a similar API to Express, it's very far from being a drop-in replacement, and implements most of the functionality differently. This creates a lot of random quirks and issues, making the switch quite difficult. Built in middlewares are also very different.
|
|
21
|
-
- `uwebsockets-express` - this library is closer to being a drop-in replacement, but misses a lot of APIs, depends on Express by calling it's methods under the hood and doesn't try to optimize routing by using native uWS router.
|
|
22
|
-
|
|
23
|
-
## Performance
|
|
24
|
-
|
|
25
|
-
### Test results
|
|
26
|
-
|
|
27
|
-
Tested using [wrk](https://github.com/wg/wrk) (`-d 60 -t 1 -c 200`). Etag was disabled in both Express and µExpress. Tested on Ubuntu 22.04, Node.js 20.17.0, AMD Ryzen 5 3600, 64GB RAM.
|
|
28
|
-
|
|
29
|
-
| Test | Express req/sec | µExpress req/sec | Express throughput | µExpress throughput | µExpress speedup |
|
|
30
|
-
| --------------------------------------------- | --------------- | ---------------- | ------------------ | ------------------- | ---------------- |
|
|
31
|
-
| routing/simple-routes (/) | 10.90k | 70.10k | 2.04 MB/sec | 11.57 MB/sec | **6.43X** |
|
|
32
|
-
| routing/lot-of-routes (/999) | 4.66k | 51.58k | 0.85 MB/sec | 8.07 MB/sec | **11.07X** |
|
|
33
|
-
| routing/some-middlewares (/90) | 10.18k | 66.97k | 1.81 MB/sec | 10.42 MB/sec | **6.58X** |
|
|
34
|
-
| routers/nested-routers (/abccc/nested/ddd) | 10.25k | 50.98k | 1.83 MB/sec | 7.98 MB/sec | **4.97X** |
|
|
35
|
-
| middlewares/express-static (/static/index.js) | 7.52k | 31.08k | 6.92 MB/sec | 26.48 MB/sec | **4.13X** |
|
|
36
|
-
| engines/ejs (/test) | 5.92k | 41.64k | 2.40 MB/sec | 16.55 MB/sec | **7.03X** |
|
|
37
|
-
| middlewares/body-urlencoded (/abc) | 8.01k | 35.10k | 1.66 MB/sec | 7.02 MB/sec | **4.38X** |
|
|
38
|
-
|
|
39
|
-
### Performance against other frameworks
|
|
40
|
-
|
|
41
|
-
Tested using [bun-http-framework-benchmark](https://github.com/dimdenGD/bun-http-framework-benchmark). This table only includes Node.js results.
|
|
42
|
-
For full table with other runtimes, check [here](https://github.com/dimdenGD/bun-http-framework-benchmark?tab=readme-ov-file#results).
|
|
43
|
-
|
|
44
|
-
| Framework | Average | Ping | Query | Body |
|
|
45
|
-
| -------------------- | ------------- | ------------- | ------------- | ------------- |
|
|
46
|
-
| uws | 94,296.49 | 108,551.92 | 104,756.22 | 69,581.33 |
|
|
47
|
-
| hyper-express | 66,356.707 | 80,002.53 | 69,953.76 | 49,113.83 |
|
|
48
|
-
| **ultimate-express** | **52,275.677** | **58,756.34** | **55,038.4** | **43,032.29** |
|
|
49
|
-
| h3 | 35,423.263 | 41,243.68 | 34,429.26 | 30,596.85 |
|
|
50
|
-
| fastify | 33,094.62 | 40,147.67 | 40,076.35 | 19,059.84 |
|
|
51
|
-
| hono | 26,576.02 | 36,215.35 | 34,656.12 | 8,856.59 |
|
|
52
|
-
| koa | 24,045.08 | 28,202.12 | 24,590.84 | 19,342.28 |
|
|
53
|
-
| express | 10,411.313 | 11,245.57 | 10,598.74 | 9,389.63 |
|
|
54
|
-
|
|
55
|
-
### Performance on real-world application
|
|
56
|
-
|
|
57
|
-
Also tested on a [real-world application](https://nekoweb.org) with templates, static files and dynamic pages with data from database, and showed 1.5-4X speedup in requests per second depending on the page.
|
|
58
|
-
|
|
59
|
-
## Differences from Express
|
|
60
|
-
|
|
61
|
-
In a lot of cases, you can just replace `require("express")` with `require("ultimate-express")` and everything works the same. But there are some differences:
|
|
62
|
-
|
|
63
|
-
- `case sensitive routing` is enabled by default.
|
|
64
|
-
- request body is only read for POST, PUT and PATCH requests by default. You can add additional methods by setting `body methods` to array with uppercased methods.
|
|
65
|
-
- For HTTPS, instead of doing this:
|
|
66
|
-
```js
|
|
67
|
-
const https = require("https");
|
|
68
|
-
const express = require("express");
|
|
69
|
-
|
|
70
|
-
const app = express();
|
|
71
|
-
|
|
72
|
-
https.createServer({
|
|
73
|
-
key: fs.readFileSync('path/to/key.pem'),
|
|
74
|
-
cert: fs.readFileSync('path/to/cert.pem')
|
|
75
|
-
}, app).listen(3000, () => {
|
|
76
|
-
console.log('Server is running on port 3000');
|
|
77
|
-
});
|
|
78
|
-
```
|
|
79
|
-
|
|
80
|
-
You have to pass `uwsOptions` to the `express()` constructor:
|
|
81
|
-
```js
|
|
82
|
-
const express = require("u-express");
|
|
83
|
-
|
|
84
|
-
const app = express({
|
|
85
|
-
uwsOptions: {
|
|
86
|
-
// https://unetworking.github.io/uWebSockets.js/generated/interfaces/AppOptions.html
|
|
87
|
-
key_file_name: 'path/to/key.pem',
|
|
88
|
-
cert_file_name: 'path/to/cert.pem'
|
|
89
|
-
}
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
app.listen(3000, () => {
|
|
93
|
-
console.log('Server is running on port 3000');
|
|
94
|
-
});
|
|
95
|
-
```
|
|
96
|
-
|
|
97
|
-
- This also applies to non-SSL HTTP too. Do not create http server manually, use `app.listen()` instead.
|
|
98
|
-
|
|
99
|
-
## Performance tips
|
|
100
|
-
|
|
101
|
-
1. µExpress tries to optimize routing as much as possible, but it's only possible if:
|
|
102
|
-
- `case sensitive routing` is enabled (it is by default, unlike in normal Express).
|
|
103
|
-
- only string paths without regex characters like *, +, (), {}, etc. can be optimized.
|
|
104
|
-
- only 1-level deep routers can be optimized.
|
|
105
|
-
|
|
106
|
-
Optimized routes can be up to 10 times faster than normal routes, as they're using native uWS router and have pre-calculated path.
|
|
107
|
-
|
|
108
|
-
2. Do not use external `serve-static` module. Instead use built-in `express.static()` middleware, which is optimized for uExpress.
|
|
109
|
-
|
|
110
|
-
3. Do not use `body-parser` module. Instead use built-in `express.text()`, `express.json()` etc.
|
|
111
|
-
|
|
112
|
-
4. Do not set `body methods` to read body of requests with GET method or other methods that don't need a body. Reading body makes server about 10k req/sec slower.
|
|
113
|
-
|
|
114
|
-
5. By default, µExpress creates 1 (or 0 if your CPU has only 1 core) child thread to improve performance of reading files. You can change this number by setting `threads` to a different number in `express()`, or set to 0 to disable thread pool (`express({ threads: 0 })`). Threads are shared between all express() instances, with largest `threads` number being used. Using more threads will not necessarily improve performance. Sometimes not using threads at all is faster, please [test](https://github.com/wg/wrk/) both options.
|
|
115
|
-
|
|
116
|
-
## WebSockets
|
|
117
|
-
|
|
118
|
-
Since you don't create http server manually, you can't properly use http.on("upgrade") to handle WebSockets. To solve this, there's currently 2 options:
|
|
119
|
-
|
|
120
|
-
- There's currently another library in works: [Ultimate WS](https://github.com/dimdenGD/ultimate-ws) that implements `ws` module. It's same concept as this library, but for WebSockets: fast drop-in replacement for `ws` module with support for uExpress upgrades.
|
|
121
|
-
- You can simply use `app.uwsApp` to access uWebSockets.js `App` instance and call its `ws()` method directly.
|
|
122
|
-
|
|
123
|
-
## Compatibility
|
|
124
|
-
|
|
125
|
-
In general, basically all features and options are supported. Use [Express 4.x documentation](https://expressjs.com/en/4x/api.html) for API reference.
|
|
126
|
-
|
|
127
|
-
✅ - Full support (all features and options are supported)
|
|
128
|
-
🚧 - Partial support (some options are not supported)
|
|
129
|
-
❌ - Not supported
|
|
130
|
-
|
|
131
|
-
### express
|
|
132
|
-
|
|
133
|
-
- ✅ express()
|
|
134
|
-
- ✅ express.Router()
|
|
135
|
-
- ✅ express.json()
|
|
136
|
-
- ✅ express.urlencoded()
|
|
137
|
-
- ✅ express.static()
|
|
138
|
-
- ✅ express.text()
|
|
139
|
-
- ✅ express.raw()
|
|
140
|
-
- 🚧 express.request (this is not a constructor but a prototype for replacing methods)
|
|
141
|
-
- 🚧 express.response (this is not a constructor but a prototype for replacing methods)
|
|
142
|
-
|
|
143
|
-
### Application
|
|
144
|
-
|
|
145
|
-
- ✅ app.listen()
|
|
146
|
-
- ✅ app.METHOD() (app.get, app.post, etc.)
|
|
147
|
-
- ✅ app.route()
|
|
148
|
-
- ✅ app.all()
|
|
149
|
-
- ✅ app.use()
|
|
150
|
-
- ✅ app.mountpath
|
|
151
|
-
- ✅ app.set()
|
|
152
|
-
- ✅ app.get()
|
|
153
|
-
- ✅ app.enable()
|
|
154
|
-
- ✅ app.disable()
|
|
155
|
-
- ✅ app.enabled()
|
|
156
|
-
- ✅ app.disabled()
|
|
157
|
-
- ✅ app.path()
|
|
158
|
-
- ✅ app.param(name, callback)
|
|
159
|
-
- ✅ app.param(callback)
|
|
160
|
-
- ✅ app.engine()
|
|
161
|
-
- ✅ app.render()
|
|
162
|
-
- ✅ app.locals
|
|
163
|
-
- ✅ app.settings
|
|
164
|
-
- ✅ app.engines
|
|
165
|
-
- ✅ app.on("mount")
|
|
166
|
-
- ✅ HEAD method
|
|
167
|
-
|
|
168
|
-
### Application settings
|
|
169
|
-
|
|
170
|
-
- ✅ case sensitive routing
|
|
171
|
-
- ✅ env
|
|
172
|
-
- ✅ etag
|
|
173
|
-
- ✅ jsonp callback name
|
|
174
|
-
- ✅ json escape
|
|
175
|
-
- ✅ json replacer
|
|
176
|
-
- ✅ json spaces
|
|
177
|
-
- ✅ query parser
|
|
178
|
-
- ✅ strict routing
|
|
179
|
-
- ✅ subdomain offset
|
|
180
|
-
- ✅ trust proxy
|
|
181
|
-
- ✅ views
|
|
182
|
-
- ✅ view cache
|
|
183
|
-
- ✅ view engine
|
|
184
|
-
- ✅ x-powered-by
|
|
185
|
-
|
|
186
|
-
### Request
|
|
187
|
-
- ✅ implements Readable stream
|
|
188
|
-
- ✅ req.app
|
|
189
|
-
- ✅ req.baseUrl
|
|
190
|
-
- ✅ req.body
|
|
191
|
-
- ✅ req.cookies
|
|
192
|
-
- ✅ req.fresh
|
|
193
|
-
- ✅ req.hostname
|
|
194
|
-
- ✅ req.headers
|
|
195
|
-
- ✅ req.headersDistinct
|
|
196
|
-
- ✅ req.rawHeaders
|
|
197
|
-
- ✅ req.ip
|
|
198
|
-
- ✅ req.ips
|
|
199
|
-
- ✅ req.method
|
|
200
|
-
- ✅ req.url
|
|
201
|
-
- ✅ req.originalUrl
|
|
202
|
-
- ✅ req.params
|
|
203
|
-
- ✅ req.path
|
|
204
|
-
- ✅ req.protocol
|
|
205
|
-
- ✅ req.query
|
|
206
|
-
- ✅ req.res
|
|
207
|
-
- ✅ req.secure
|
|
208
|
-
- ✅ req.signedCookies
|
|
209
|
-
- ✅ req.stale
|
|
210
|
-
- ✅ req.subdomains
|
|
211
|
-
- ✅ req.xhr
|
|
212
|
-
- 🚧 req.route (route implementation is different from Express)
|
|
213
|
-
- 🚧 req.connection, req.socket (only `encrypted`, `remoteAddress`, `localPort` and `remotePort` are supported)
|
|
214
|
-
- ✅ req.accepts()
|
|
215
|
-
- ✅ req.acceptsCharsets()
|
|
216
|
-
- ✅ req.acceptsEncodings()
|
|
217
|
-
- ✅ req.acceptsLanguages()
|
|
218
|
-
- ✅ req.get()
|
|
219
|
-
- ✅ req.is()
|
|
220
|
-
- ✅ req.param()
|
|
221
|
-
- ✅ req.range()
|
|
222
|
-
|
|
223
|
-
### Response
|
|
224
|
-
|
|
225
|
-
- ✅ implements Writable stream
|
|
226
|
-
- ✅ res.app
|
|
227
|
-
- ✅ res.headersSent
|
|
228
|
-
- ✅ res.req
|
|
229
|
-
- ✅ res.locals
|
|
230
|
-
- ✅ res.append()
|
|
231
|
-
- ✅ res.attachment()
|
|
232
|
-
- ✅ res.cookie()
|
|
233
|
-
- ✅ res.clearCookie()
|
|
234
|
-
- ✅ res.download()
|
|
235
|
-
- ✅ res.end()
|
|
236
|
-
- ✅ res.format()
|
|
237
|
-
- ✅ res.getHeader(), res.get()
|
|
238
|
-
- ✅ res.json()
|
|
239
|
-
- ✅ res.jsonp()
|
|
240
|
-
- ✅ res.links()
|
|
241
|
-
- ✅ res.location()
|
|
242
|
-
- ✅ res.redirect()
|
|
243
|
-
- ✅ res.render()
|
|
244
|
-
- ✅ res.send()
|
|
245
|
-
- ✅ res.sendFile()
|
|
246
|
-
- - ✅ options.maxAge
|
|
247
|
-
- - ✅ options.root
|
|
248
|
-
- - ✅ options.lastModified
|
|
249
|
-
- - ✅ options.headers
|
|
250
|
-
- - ✅ options.dotfiles
|
|
251
|
-
- - ✅ options.acceptRanges
|
|
252
|
-
- - ✅ options.cacheControl
|
|
253
|
-
- - ✅ options.immutable
|
|
254
|
-
- - ✅ Range header
|
|
255
|
-
- - ✅ Setting ETag header
|
|
256
|
-
- - ✅ If-Match header
|
|
257
|
-
- - ✅ If-Modified-Since header
|
|
258
|
-
- - ✅ If-Unmodified-Since header
|
|
259
|
-
- - ✅ If-Range header
|
|
260
|
-
- ✅ res.sendStatus()
|
|
261
|
-
- ✅ res.header(), res.setHeader(), res.set()
|
|
262
|
-
- ✅ res.status()
|
|
263
|
-
- ✅ res.type()
|
|
264
|
-
- ✅ res.vary()
|
|
265
|
-
- ✅ res.removeHeader()
|
|
266
|
-
- ✅ res.write()
|
|
267
|
-
- ✅ res.writeHead()
|
|
268
|
-
|
|
269
|
-
### Router
|
|
270
|
-
|
|
271
|
-
- ✅ router.all()
|
|
272
|
-
- ✅ router.METHOD() (router.get, router.post, etc.)
|
|
273
|
-
- ✅ router.route()
|
|
274
|
-
- ✅ router.use()
|
|
275
|
-
- ✅ router.param(name, callback)
|
|
276
|
-
- ✅ router.param(callback)
|
|
277
|
-
- ✅ options.caseSensitive
|
|
278
|
-
- ✅ options.strict
|
|
279
|
-
- ✅ options.mergeParams
|
|
280
|
-
|
|
281
|
-
## Tested middlewares
|
|
282
|
-
|
|
283
|
-
Most of the middlewares that are compatible with Express are compatible with µExpress. Here's list of middlewares that we test for compatibility:
|
|
284
|
-
|
|
285
|
-
- ✅ [body-parser](https://npmjs.com/package/body-parser) (use `express.text()` etc instead for better performance)
|
|
286
|
-
- ✅ [cookie-parser](https://npmjs.com/package/cookie-parser)
|
|
287
|
-
- ✅ [cookie-session](https://npmjs.com/package/cookie-session)
|
|
288
|
-
- ✅ [serve-static](https://npmjs.com/package/serve-static) (use `express.static()` instead for better performance)
|
|
289
|
-
- ✅ [serve-index](https://npmjs.com/package/serve-index)
|
|
290
|
-
- ✅ [cors](https://npmjs.com/package/cors)
|
|
291
|
-
- ✅ [errorhandler](https://npmjs.com/package/errorhandler)
|
|
292
|
-
- ✅ [method-override](https://npmjs.com/package/method-override)
|
|
293
|
-
- ✅ [multer](https://npmjs.com/package/multer)
|
|
294
|
-
- ✅ [response-time](https://npmjs.com/package/response-time)
|
|
295
|
-
- ✅ [express-fileupload](https://npmjs.com/package/express-fileupload)
|
|
296
|
-
- ✅ [express-session](https://npmjs.com/package/express-session)
|
|
297
|
-
- ✅ [express-rate-limit](https://npmjs.com/package/express-rate-limit)
|
|
298
|
-
- ✅ [vhost](https://npmjs.com/package/vhost)
|
|
299
|
-
|
|
300
|
-
Middlewares that are confirmed to not work:
|
|
301
|
-
|
|
302
|
-
- ❌ [compression](https://npmjs.com/package/compression) (doesn't error, but doesn't compress)
|
|
303
|
-
|
|
304
|
-
## Tested view engines
|
|
305
|
-
|
|
306
|
-
Any Express view engine should work. Here's list of engines we include in our test suite:
|
|
307
|
-
|
|
308
|
-
- ✅ [ejs](https://npmjs.com/package/ejs)
|
|
309
|
-
- ✅ [pug](https://npmjs.com/package/pug)
|
|
310
|
-
- ✅ [express-dot-engine](https://npmjs.com/package/express-dot-engine)
|
|
311
|
-
- ✅ [express-art-template](https://npmjs.com/package/express-art-template)
|
|
312
|
-
- ✅ [express-handlebars](https://npmjs.com/package/express-handlebars)
|
|
1
|
+
# µExpress / Ultimate Express
|
|
2
|
+
|
|
3
|
+
The *Ultimate* Express. Fastest http server with **full** Express compatibility, based on µWebSockets.
|
|
4
|
+
|
|
5
|
+
This library is a very fast re-implementation of Express.js 4.
|
|
6
|
+
It is designed to be a drop-in replacement for Express.js, with the same API and functionality, while being much faster. It is not a fork of Express.js.
|
|
7
|
+
To make sure µExpress matches behavior of Express in all cases, we run all tests with Express first, and then with µExpress and compare results to make sure they match.
|
|
8
|
+
|
|
9
|
+
`npm install ultimate-express` -> replace `express` with `ultimate-express` -> done[*](https://github.com/dimdenGD/ultimate-express?tab=readme-ov-file#differences-from-express)
|
|
10
|
+
|
|
11
|
+
[](https://nodejs.org)
|
|
12
|
+
[](https://npmjs.com/package/ultimate-express)
|
|
13
|
+
[](https://patreon.com/dimdendev)
|
|
14
|
+
|
|
15
|
+
## Difference from similar projects
|
|
16
|
+
|
|
17
|
+
Similar projects based on uWebSockets:
|
|
18
|
+
|
|
19
|
+
- `express` on Bun - since Bun uses uWS for its HTTP module, Express is about 2-3 times faster than on Node.js, but still slower than µExpress because it doesn't do uWS-specific optimizations.
|
|
20
|
+
- `hyper-express` - while having a similar API to Express, it's very far from being a drop-in replacement, and implements most of the functionality differently. This creates a lot of random quirks and issues, making the switch quite difficult. Built in middlewares are also very different.
|
|
21
|
+
- `uwebsockets-express` - this library is closer to being a drop-in replacement, but misses a lot of APIs, depends on Express by calling it's methods under the hood and doesn't try to optimize routing by using native uWS router.
|
|
22
|
+
|
|
23
|
+
## Performance
|
|
24
|
+
|
|
25
|
+
### Test results
|
|
26
|
+
|
|
27
|
+
Tested using [wrk](https://github.com/wg/wrk) (`-d 60 -t 1 -c 200`). Etag was disabled in both Express and µExpress. Tested on Ubuntu 22.04, Node.js 20.17.0, AMD Ryzen 5 3600, 64GB RAM.
|
|
28
|
+
|
|
29
|
+
| Test | Express req/sec | µExpress req/sec | Express throughput | µExpress throughput | µExpress speedup |
|
|
30
|
+
| --------------------------------------------- | --------------- | ---------------- | ------------------ | ------------------- | ---------------- |
|
|
31
|
+
| routing/simple-routes (/) | 10.90k | 70.10k | 2.04 MB/sec | 11.57 MB/sec | **6.43X** |
|
|
32
|
+
| routing/lot-of-routes (/999) | 4.66k | 51.58k | 0.85 MB/sec | 8.07 MB/sec | **11.07X** |
|
|
33
|
+
| routing/some-middlewares (/90) | 10.18k | 66.97k | 1.81 MB/sec | 10.42 MB/sec | **6.58X** |
|
|
34
|
+
| routers/nested-routers (/abccc/nested/ddd) | 10.25k | 50.98k | 1.83 MB/sec | 7.98 MB/sec | **4.97X** |
|
|
35
|
+
| middlewares/express-static (/static/index.js) | 7.52k | 31.08k | 6.92 MB/sec | 26.48 MB/sec | **4.13X** |
|
|
36
|
+
| engines/ejs (/test) | 5.92k | 41.64k | 2.40 MB/sec | 16.55 MB/sec | **7.03X** |
|
|
37
|
+
| middlewares/body-urlencoded (/abc) | 8.01k | 35.10k | 1.66 MB/sec | 7.02 MB/sec | **4.38X** |
|
|
38
|
+
|
|
39
|
+
### Performance against other frameworks
|
|
40
|
+
|
|
41
|
+
Tested using [bun-http-framework-benchmark](https://github.com/dimdenGD/bun-http-framework-benchmark). This table only includes Node.js results.
|
|
42
|
+
For full table with other runtimes, check [here](https://github.com/dimdenGD/bun-http-framework-benchmark?tab=readme-ov-file#results).
|
|
43
|
+
|
|
44
|
+
| Framework | Average | Ping | Query | Body |
|
|
45
|
+
| -------------------- | ------------- | ------------- | ------------- | ------------- |
|
|
46
|
+
| uws | 94,296.49 | 108,551.92 | 104,756.22 | 69,581.33 |
|
|
47
|
+
| hyper-express | 66,356.707 | 80,002.53 | 69,953.76 | 49,113.83 |
|
|
48
|
+
| **ultimate-express** | **52,275.677** | **58,756.34** | **55,038.4** | **43,032.29** |
|
|
49
|
+
| h3 | 35,423.263 | 41,243.68 | 34,429.26 | 30,596.85 |
|
|
50
|
+
| fastify | 33,094.62 | 40,147.67 | 40,076.35 | 19,059.84 |
|
|
51
|
+
| hono | 26,576.02 | 36,215.35 | 34,656.12 | 8,856.59 |
|
|
52
|
+
| koa | 24,045.08 | 28,202.12 | 24,590.84 | 19,342.28 |
|
|
53
|
+
| express | 10,411.313 | 11,245.57 | 10,598.74 | 9,389.63 |
|
|
54
|
+
|
|
55
|
+
### Performance on real-world application
|
|
56
|
+
|
|
57
|
+
Also tested on a [real-world application](https://nekoweb.org) with templates, static files and dynamic pages with data from database, and showed 1.5-4X speedup in requests per second depending on the page.
|
|
58
|
+
|
|
59
|
+
## Differences from Express
|
|
60
|
+
|
|
61
|
+
In a lot of cases, you can just replace `require("express")` with `require("ultimate-express")` and everything works the same. But there are some differences:
|
|
62
|
+
|
|
63
|
+
- `case sensitive routing` is enabled by default.
|
|
64
|
+
- request body is only read for POST, PUT and PATCH requests by default. You can add additional methods by setting `body methods` to array with uppercased methods.
|
|
65
|
+
- For HTTPS, instead of doing this:
|
|
66
|
+
```js
|
|
67
|
+
const https = require("https");
|
|
68
|
+
const express = require("express");
|
|
69
|
+
|
|
70
|
+
const app = express();
|
|
71
|
+
|
|
72
|
+
https.createServer({
|
|
73
|
+
key: fs.readFileSync('path/to/key.pem'),
|
|
74
|
+
cert: fs.readFileSync('path/to/cert.pem')
|
|
75
|
+
}, app).listen(3000, () => {
|
|
76
|
+
console.log('Server is running on port 3000');
|
|
77
|
+
});
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
You have to pass `uwsOptions` to the `express()` constructor:
|
|
81
|
+
```js
|
|
82
|
+
const express = require("u-express");
|
|
83
|
+
|
|
84
|
+
const app = express({
|
|
85
|
+
uwsOptions: {
|
|
86
|
+
// https://unetworking.github.io/uWebSockets.js/generated/interfaces/AppOptions.html
|
|
87
|
+
key_file_name: 'path/to/key.pem',
|
|
88
|
+
cert_file_name: 'path/to/cert.pem'
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
app.listen(3000, () => {
|
|
93
|
+
console.log('Server is running on port 3000');
|
|
94
|
+
});
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
- This also applies to non-SSL HTTP too. Do not create http server manually, use `app.listen()` instead.
|
|
98
|
+
|
|
99
|
+
## Performance tips
|
|
100
|
+
|
|
101
|
+
1. µExpress tries to optimize routing as much as possible, but it's only possible if:
|
|
102
|
+
- `case sensitive routing` is enabled (it is by default, unlike in normal Express).
|
|
103
|
+
- only string paths without regex characters like *, +, (), {}, etc. can be optimized.
|
|
104
|
+
- only 1-level deep routers can be optimized.
|
|
105
|
+
|
|
106
|
+
Optimized routes can be up to 10 times faster than normal routes, as they're using native uWS router and have pre-calculated path.
|
|
107
|
+
|
|
108
|
+
2. Do not use external `serve-static` module. Instead use built-in `express.static()` middleware, which is optimized for uExpress.
|
|
109
|
+
|
|
110
|
+
3. Do not use `body-parser` module. Instead use built-in `express.text()`, `express.json()` etc.
|
|
111
|
+
|
|
112
|
+
4. Do not set `body methods` to read body of requests with GET method or other methods that don't need a body. Reading body makes server about 10k req/sec slower.
|
|
113
|
+
|
|
114
|
+
5. By default, µExpress creates 1 (or 0 if your CPU has only 1 core) child thread to improve performance of reading files. You can change this number by setting `threads` to a different number in `express()`, or set to 0 to disable thread pool (`express({ threads: 0 })`). Threads are shared between all express() instances, with largest `threads` number being used. Using more threads will not necessarily improve performance. Sometimes not using threads at all is faster, please [test](https://github.com/wg/wrk/) both options.
|
|
115
|
+
|
|
116
|
+
## WebSockets
|
|
117
|
+
|
|
118
|
+
Since you don't create http server manually, you can't properly use http.on("upgrade") to handle WebSockets. To solve this, there's currently 2 options:
|
|
119
|
+
|
|
120
|
+
- There's currently another library in works: [Ultimate WS](https://github.com/dimdenGD/ultimate-ws) that implements `ws` module. It's same concept as this library, but for WebSockets: fast drop-in replacement for `ws` module with support for uExpress upgrades.
|
|
121
|
+
- You can simply use `app.uwsApp` to access uWebSockets.js `App` instance and call its `ws()` method directly.
|
|
122
|
+
|
|
123
|
+
## Compatibility
|
|
124
|
+
|
|
125
|
+
In general, basically all features and options are supported. Use [Express 4.x documentation](https://expressjs.com/en/4x/api.html) for API reference.
|
|
126
|
+
|
|
127
|
+
✅ - Full support (all features and options are supported)
|
|
128
|
+
🚧 - Partial support (some options are not supported)
|
|
129
|
+
❌ - Not supported
|
|
130
|
+
|
|
131
|
+
### express
|
|
132
|
+
|
|
133
|
+
- ✅ express()
|
|
134
|
+
- ✅ express.Router()
|
|
135
|
+
- ✅ express.json()
|
|
136
|
+
- ✅ express.urlencoded()
|
|
137
|
+
- ✅ express.static()
|
|
138
|
+
- ✅ express.text()
|
|
139
|
+
- ✅ express.raw()
|
|
140
|
+
- 🚧 express.request (this is not a constructor but a prototype for replacing methods)
|
|
141
|
+
- 🚧 express.response (this is not a constructor but a prototype for replacing methods)
|
|
142
|
+
|
|
143
|
+
### Application
|
|
144
|
+
|
|
145
|
+
- ✅ app.listen()
|
|
146
|
+
- ✅ app.METHOD() (app.get, app.post, etc.)
|
|
147
|
+
- ✅ app.route()
|
|
148
|
+
- ✅ app.all()
|
|
149
|
+
- ✅ app.use()
|
|
150
|
+
- ✅ app.mountpath
|
|
151
|
+
- ✅ app.set()
|
|
152
|
+
- ✅ app.get()
|
|
153
|
+
- ✅ app.enable()
|
|
154
|
+
- ✅ app.disable()
|
|
155
|
+
- ✅ app.enabled()
|
|
156
|
+
- ✅ app.disabled()
|
|
157
|
+
- ✅ app.path()
|
|
158
|
+
- ✅ app.param(name, callback)
|
|
159
|
+
- ✅ app.param(callback)
|
|
160
|
+
- ✅ app.engine()
|
|
161
|
+
- ✅ app.render()
|
|
162
|
+
- ✅ app.locals
|
|
163
|
+
- ✅ app.settings
|
|
164
|
+
- ✅ app.engines
|
|
165
|
+
- ✅ app.on("mount")
|
|
166
|
+
- ✅ HEAD method
|
|
167
|
+
|
|
168
|
+
### Application settings
|
|
169
|
+
|
|
170
|
+
- ✅ case sensitive routing
|
|
171
|
+
- ✅ env
|
|
172
|
+
- ✅ etag
|
|
173
|
+
- ✅ jsonp callback name
|
|
174
|
+
- ✅ json escape
|
|
175
|
+
- ✅ json replacer
|
|
176
|
+
- ✅ json spaces
|
|
177
|
+
- ✅ query parser
|
|
178
|
+
- ✅ strict routing
|
|
179
|
+
- ✅ subdomain offset
|
|
180
|
+
- ✅ trust proxy
|
|
181
|
+
- ✅ views
|
|
182
|
+
- ✅ view cache
|
|
183
|
+
- ✅ view engine
|
|
184
|
+
- ✅ x-powered-by
|
|
185
|
+
|
|
186
|
+
### Request
|
|
187
|
+
- ✅ implements Readable stream
|
|
188
|
+
- ✅ req.app
|
|
189
|
+
- ✅ req.baseUrl
|
|
190
|
+
- ✅ req.body
|
|
191
|
+
- ✅ req.cookies
|
|
192
|
+
- ✅ req.fresh
|
|
193
|
+
- ✅ req.hostname
|
|
194
|
+
- ✅ req.headers
|
|
195
|
+
- ✅ req.headersDistinct
|
|
196
|
+
- ✅ req.rawHeaders
|
|
197
|
+
- ✅ req.ip
|
|
198
|
+
- ✅ req.ips
|
|
199
|
+
- ✅ req.method
|
|
200
|
+
- ✅ req.url
|
|
201
|
+
- ✅ req.originalUrl
|
|
202
|
+
- ✅ req.params
|
|
203
|
+
- ✅ req.path
|
|
204
|
+
- ✅ req.protocol
|
|
205
|
+
- ✅ req.query
|
|
206
|
+
- ✅ req.res
|
|
207
|
+
- ✅ req.secure
|
|
208
|
+
- ✅ req.signedCookies
|
|
209
|
+
- ✅ req.stale
|
|
210
|
+
- ✅ req.subdomains
|
|
211
|
+
- ✅ req.xhr
|
|
212
|
+
- 🚧 req.route (route implementation is different from Express)
|
|
213
|
+
- 🚧 req.connection, req.socket (only `encrypted`, `remoteAddress`, `localPort` and `remotePort` are supported)
|
|
214
|
+
- ✅ req.accepts()
|
|
215
|
+
- ✅ req.acceptsCharsets()
|
|
216
|
+
- ✅ req.acceptsEncodings()
|
|
217
|
+
- ✅ req.acceptsLanguages()
|
|
218
|
+
- ✅ req.get()
|
|
219
|
+
- ✅ req.is()
|
|
220
|
+
- ✅ req.param()
|
|
221
|
+
- ✅ req.range()
|
|
222
|
+
|
|
223
|
+
### Response
|
|
224
|
+
|
|
225
|
+
- ✅ implements Writable stream
|
|
226
|
+
- ✅ res.app
|
|
227
|
+
- ✅ res.headersSent
|
|
228
|
+
- ✅ res.req
|
|
229
|
+
- ✅ res.locals
|
|
230
|
+
- ✅ res.append()
|
|
231
|
+
- ✅ res.attachment()
|
|
232
|
+
- ✅ res.cookie()
|
|
233
|
+
- ✅ res.clearCookie()
|
|
234
|
+
- ✅ res.download()
|
|
235
|
+
- ✅ res.end()
|
|
236
|
+
- ✅ res.format()
|
|
237
|
+
- ✅ res.getHeader(), res.get()
|
|
238
|
+
- ✅ res.json()
|
|
239
|
+
- ✅ res.jsonp()
|
|
240
|
+
- ✅ res.links()
|
|
241
|
+
- ✅ res.location()
|
|
242
|
+
- ✅ res.redirect()
|
|
243
|
+
- ✅ res.render()
|
|
244
|
+
- ✅ res.send()
|
|
245
|
+
- ✅ res.sendFile()
|
|
246
|
+
- - ✅ options.maxAge
|
|
247
|
+
- - ✅ options.root
|
|
248
|
+
- - ✅ options.lastModified
|
|
249
|
+
- - ✅ options.headers
|
|
250
|
+
- - ✅ options.dotfiles
|
|
251
|
+
- - ✅ options.acceptRanges
|
|
252
|
+
- - ✅ options.cacheControl
|
|
253
|
+
- - ✅ options.immutable
|
|
254
|
+
- - ✅ Range header
|
|
255
|
+
- - ✅ Setting ETag header
|
|
256
|
+
- - ✅ If-Match header
|
|
257
|
+
- - ✅ If-Modified-Since header
|
|
258
|
+
- - ✅ If-Unmodified-Since header
|
|
259
|
+
- - ✅ If-Range header
|
|
260
|
+
- ✅ res.sendStatus()
|
|
261
|
+
- ✅ res.header(), res.setHeader(), res.set()
|
|
262
|
+
- ✅ res.status()
|
|
263
|
+
- ✅ res.type()
|
|
264
|
+
- ✅ res.vary()
|
|
265
|
+
- ✅ res.removeHeader()
|
|
266
|
+
- ✅ res.write()
|
|
267
|
+
- ✅ res.writeHead()
|
|
268
|
+
|
|
269
|
+
### Router
|
|
270
|
+
|
|
271
|
+
- ✅ router.all()
|
|
272
|
+
- ✅ router.METHOD() (router.get, router.post, etc.)
|
|
273
|
+
- ✅ router.route()
|
|
274
|
+
- ✅ router.use()
|
|
275
|
+
- ✅ router.param(name, callback)
|
|
276
|
+
- ✅ router.param(callback)
|
|
277
|
+
- ✅ options.caseSensitive
|
|
278
|
+
- ✅ options.strict
|
|
279
|
+
- ✅ options.mergeParams
|
|
280
|
+
|
|
281
|
+
## Tested middlewares
|
|
282
|
+
|
|
283
|
+
Most of the middlewares that are compatible with Express are compatible with µExpress. Here's list of middlewares that we test for compatibility:
|
|
284
|
+
|
|
285
|
+
- ✅ [body-parser](https://npmjs.com/package/body-parser) (use `express.text()` etc instead for better performance)
|
|
286
|
+
- ✅ [cookie-parser](https://npmjs.com/package/cookie-parser)
|
|
287
|
+
- ✅ [cookie-session](https://npmjs.com/package/cookie-session)
|
|
288
|
+
- ✅ [serve-static](https://npmjs.com/package/serve-static) (use `express.static()` instead for better performance)
|
|
289
|
+
- ✅ [serve-index](https://npmjs.com/package/serve-index)
|
|
290
|
+
- ✅ [cors](https://npmjs.com/package/cors)
|
|
291
|
+
- ✅ [errorhandler](https://npmjs.com/package/errorhandler)
|
|
292
|
+
- ✅ [method-override](https://npmjs.com/package/method-override)
|
|
293
|
+
- ✅ [multer](https://npmjs.com/package/multer)
|
|
294
|
+
- ✅ [response-time](https://npmjs.com/package/response-time)
|
|
295
|
+
- ✅ [express-fileupload](https://npmjs.com/package/express-fileupload)
|
|
296
|
+
- ✅ [express-session](https://npmjs.com/package/express-session)
|
|
297
|
+
- ✅ [express-rate-limit](https://npmjs.com/package/express-rate-limit)
|
|
298
|
+
- ✅ [vhost](https://npmjs.com/package/vhost)
|
|
299
|
+
|
|
300
|
+
Middlewares that are confirmed to not work:
|
|
301
|
+
|
|
302
|
+
- ❌ [compression](https://npmjs.com/package/compression) (doesn't error, but doesn't compress)
|
|
303
|
+
|
|
304
|
+
## Tested view engines
|
|
305
|
+
|
|
306
|
+
Any Express view engine should work. Here's list of engines we include in our test suite:
|
|
307
|
+
|
|
308
|
+
- ✅ [ejs](https://npmjs.com/package/ejs)
|
|
309
|
+
- ✅ [pug](https://npmjs.com/package/pug)
|
|
310
|
+
- ✅ [express-dot-engine](https://npmjs.com/package/express-dot-engine)
|
|
311
|
+
- ✅ [express-art-template](https://npmjs.com/package/express-art-template)
|
|
312
|
+
- ✅ [express-handlebars](https://npmjs.com/package/express-handlebars)
|
|
313
313
|
- ✅ [swig](https://npmjs.com/package/swig)
|