https://github.com/rtritto/uws-wrapper
Archived: Wrapper for uWebSockets.js
https://github.com/rtritto/uws-wrapper
http http-server nodejs plugin proxy-protocol typescript uwebsocketsjs uws websockets wrapper
Last synced: 3 months ago
JSON representation
Archived: Wrapper for uWebSockets.js
- Host: GitHub
- URL: https://github.com/rtritto/uws-wrapper
- Owner: rtritto
- License: mit
- Archived: true
- Created: 2024-10-11T14:31:57.000Z (8 months ago)
- Default Branch: master
- Last Pushed: 2024-10-15T12:18:54.000Z (8 months ago)
- Last Synced: 2025-02-20T20:37:52.884Z (3 months ago)
- Topics: http, http-server, nodejs, plugin, proxy-protocol, typescript, uwebsocketsjs, uws, websockets, wrapper
- Language: TypeScript
- Homepage:
- Size: 650 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ARCHIVED
Use [uws-utils](https://github.com/rtritto/uws-common) instead# uws-wrapper
Plugin for [uWebSockets.js](https://github.com/uNetworking/uWebSockets.js) that wraps _App_ instance to improve DX (Developer Experience).
## Installation
```sh
yarn add uws-wrapper
```## Changes/Features:
- Replace the parameters in the callback of the http request functions with a context object:```diff
-app[](, (res, req) => { ... })
+app[](, ({ req, res }) => { ... })
```
- Replace the result of `res.getQuery()` method with a query object (generated by [fastest-qs](https://github.com/rtritto/fastest-qs)):```diff
const query = req.getQuery()
-console.log('Query:', query) // q=1&q2
+console.log('Query:', query) // { q: 1, q: 2 }
```
- Add `req.body.json` method```ts
const jsonBody = await req.body.json()
console.log(jsonBody) // { prop1: 1, prop2: 2 }
```## Register the Plugin / Usage
```ts
import { App } from 'uWebSockets.js'
import { transformCallback } from 'uws-wrapper'const port = +(process.env.PORT || 3000)
// Register the Plugin
const app = transformCallback({
// Default HTTP methods: 'get', 'post', 'options', 'del', 'patch', 'put', 'head', 'connect', 'trace', 'any'
httpMethods: new Set(['get', 'post'])
})(App())// Usage
app
.get(pattern, async ({ req, res }) => {
console.log('Query:', req.getQuery()) // { q: 1, q: 2 }
console.log('JSON body:', await req.body.json()) // { prop1: 1, prop2: 2 }
res.end('Hello World!')
})
.listen(port, (listenSocket) => {
if (listenSocket) {
console.log(`Server running at http://localhost:${port}`)
} else {
console.log(`Failed to listen to port ${port}`)
}
})
```## License
This project is licensed under the [MIT License](LICENSE).