The tiniest middleware library for node's HTTP
Invisible if you squint hard enough
I've written this in my experimentals sink for over 1.5 years now, the first public version of the same is available here barelyhuman/http/middleware.
I made it to be small and contained to reduce the middleware's load on node's server.
With respect to:
; npm i --save @barelyhuman/tiny-use
To use with the default node:http
/http
API, you use it just like any other
express js middleware, the difference being that the middleware now supports
async flow
import http from 'node:http'
import { use } from '@barelyhuman/tiny-use'
http.createServer(
use(
async (req, res, next) => {
req.value = 1
await next()
console.log({
value: req.value,
})
},
async (req, res, next) => {
req.value = 2
res.end('Final Message')
return
}
)
)
You don't have to rebuild an ecosystem, most of what connect
has and what
express uses should be usable.
If you work with network functions that are more inclined to the Fetch API standard and you need to use common middleware instead of manually calling them in each function call, you can do so by making minor modifications to the import
import { use } from '@barelyhuman/tiny-use/fetch'
const withMiddleware = use(
req => {
req.userId = 1
},
req => {
return new Response(req.userId)
}
)
export default {
fetch: withMiddleware,
}
[!NOTE] When using the fetch based API, you might need to also add a bundling step if the tool you use doesn't do the bundling for you