Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

uws-connect

spurreiter111MIT1.4.0TypeScript support: included

Use connect like middlewares with uWebSockets.js

uws, uwebsocketsjs, websockets, connect, express, expressjs, fast, http-server, https-server, http, https, ws, websocket, performance, router

readme

uws-connect

Use connect like middlewares with uWebSockets.js.

Provides support for

  • connect helper for connecting connect, express middlewares
  • body-parser (json, form-urlencoded)
  • nodejs streams support for uWS.HttpRequest and uWS.HttpResponse (may not be 100% compliant with nodejs streams)
  • final handler for errors

The design aims to be as fast and unopinionated as possible.

All parts provided can also be used as single building blocks.

Table of Contents

Installation

npm install uws-connect

Usage

import { App, bodyParser, params } from 'uws-connect'
import { Transform } from 'stream'

const app = App()

app.use((req, res, next) => { // a simple logging mw applied to all routes
  const { method, url } = req
  console.log('%s %s', method, url)
  next()
})
app.get('/',
  // a connect like middleware
  (req, res, next) => {
    next()
    },
  // async middleware (no need for `next()` or `try {} catch (err) {}`)
  async (req, res) => {
    res.body = await something()
  },
  (req, res) => {
    res.end(res.body)
  }
)
app.put('/users/:user', // does req.params parsing like with express.
  // does json or form body parsing.
  bodyParser({ limit: 100000 }),
  (req, res) => {
    // restana like res.send method
    // res.send(data: any, status?: number, headers?: object) => void
    res.send({
      params: req.params,
      body: req.body,     // from `bodyParser()` middleware
    })
  }
)

// if stream support is needed...
const transform = new Transform({
  transform (chunk, enc, cb) {
    this.push(_chunk)
    cb()
  }
})
app.post('/echo',
  (req, res) => { req.pipe(transform).pipe(res) }
)

app.listen(9001)

If you need to better fine tune the performance of your app and don't want to trade speed, use glue(...handlers) for connecting middlewares.

// same as `import uWs from 'uWebSockets.js'`
import { uWS, connect, params } from 'uws-connect'
import cors from 'cors'
const uwsApp = uWS.App()

// just uWS, as fast as fast can be
// NOTE: (response, request) for uWS handler
uwsApp.get('/', (response, request) => response.end('done'))

// use some routes with connect middlewares (like cors)
// NOTE: glue() uses express (req, res, next) handlers!
const _cors = cors()
const glue = connect()
uwsApp.options('/*', glue(_cors))
uwsApp.get('/with-cors/:param', glue(
  _cors,
  params('/with-cors/:param'), // must use the same path as the route
  (req, res) => res.end(`with cors - ${req.params.param}`))
)

uwsApp.listen(9001, () => {})

Benchmarks

$ cd benchmark
$ node index.js -d 10 -c 2500 -p 4

*) Results may vary on your machine.

Package Version Requests/s Latency (ms) Throughput (Mb)
uWebSockets 20.52.0 365266 45.84 37.27
uws-connect 1.4.0 225102 52.39 19.32
native 24.4.1 125107 44.20 16.34
polka 0.5.2 119533 44.85 15.62
restana 5.0.0 109485 60.59 14.31
express 5.1.0 87974 58.47 11.49

Contributing

Your help is appreciated. File an issue and fork this project to contribute with your ideas.

Please follow the minimalistic approach as chosen here. Keep things simple.

If you contribute code to this project, you are implicitly allowing your code to be distributed under the MIT license. You are also implicitly verifying that all code is your original work or correctly attributed with the source of its origin and license.

The Code-of-Conduct is Contributor Covenant Code of Conduct.

License

MIT Licensed

References

changelog

1.4.0 (2025-07-16)

  • feat: include params parser in app route handlers (#5698285)
  • chore: bump benchmark dependencies (#5b9cbc4)

1.3.0 (2025-07-16)

  • chore: engine support node >=20 <=24
  • chore: bump dependencies (#ceea67c)
  • chore: fix types (#b01c65e)
  • chore: bump devDependencies (#b1ad4fc)

1.2.6 (2025-02-11)

  • chore: bump dependencies (#7fbece2)

1.2.5 (2024-11-10)

  • chore: bump dependencies; update cookie (#b512ad5)

1.2.4 (2024-07-13)

  • chore: update benchmark (#2cd517d)
  • test: websocket test for node >= 22 (#ec554c0)
  • chore: use prettier and eslint@9 (#ed13978)

1.2.3 (2024-03-12)

  • chore: bump dependencies (#b586572)
  • docs: update benchmark results (#7ee88b1)

1.2.2 (2023-12-29)

  • chore: bump dependencies (#f76c8ad)
  • chore: remove package lock (#42ca1cf)

1.2.1 (2023-10-21)

  • chore(benchmakr): bump dependencies (#cb0130e)
  • chore: bump dependencies (#d62fccd)
  • chore: bump dependencies (#76d825e)

1.2.0 (2023-06-16)

  • test: qs parser add test cases (#1c8fde8)
  • feat: app.use() pre-handlers for http routes (#a06f728)

1.1.5 (2023-06-16)

  • chore: fix type generation (#bd6fd8b)
  • chore: benchmark (#c4b7dc1)
  • fix: node@20 and corking (#1dce802)
  • fix: linter issues (#77d666b)
  • test: uws@20.15 adds date header to response (#4cc3481)

1.1.4 (2022-12-04)

  • chore: bump dependencies (#8ba01c9)
  • docs: fix code sample (#6a06984)

1.1.3 (2022-10-29)

  • docs: update benchmark results (#df0f499)
  • chore: bump dependencies (#cb497e7)
  • chore: bump dependencies (#81963a5)
  • chore(benchmark): bump dependencies (#69c447f)
  • chore: bump dependencies (#f1abf93)

1.1.2 (2022-07-13)

  • fix(bodyParser): replace qs with UrlSearchParams parser (#3503573)
  • chore: bump dependencies (#de4f872)
  • fix: request query parser (#67127d8)

1.1.1 (2022-06-11)

  • fix: querystring parsing (#e4c1ff2)
  • fix: req.protocol (#6a4d6b5)
  • docs: App types for all methods (#38a583e)

1.1.0 (2022-06-04)

  • refactor: remove qs and use URLSearchParams for query string parsing (#9c4e1a5)
  • docs: res.end (#a3768a1)
  • refactor: rename uws Request, Response arguments (#2b44760)
  • refactor: Response.send (#95181e7)
  • chore: fix dependabot (#3d8933c)
  • Create dependabot.yml (#ecfa52a)

1.0.1 (2022-05-27)

  • fix: App types (#8607a94)
  • feat(benchmark): restana (#ee7f797)
  • fix: uWS export and types (#c906c55)
  • chore: bump dependencies (#30f34b6)
  • fix: allow request url overwrite (#6aab858)
  • fix: silent mode config (#118bfe8)

1.0.0 (2022-05-07)

  • feat: first version (#557f0d5)
  • initial commit (#c50eea8)