To run minimal and fast rest api on the edges.
pnpm i restfun
bun add restfun
Hello word with restfun
:
import restfun from 'restfun'
const server = restfun()
server.get('/', (req, res) => {
res.html('Hello world')
})
server.listen(3001)
restfun()
restfun(Object options)
cors
: object, headers for cors, default to{}
noDelay
: bolean, default totrue
keepAlive
: bolean, default tofalse
maxHeaderSize
: number, default to16384
headersTimeout
: number, default to60000
requestTimeout
: number, default to300000
Regarding cors
, by default, restfun
enalbe CORS by auto adding the following headers:
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: authorization, token, x-token, apikey, x-api-key
Developer can modify, or add more via cors
options, for example:
import restfun from 'restfun'
export const server = restfun({
cors: {
'Access-Control-Allow-Origin': 'https://myownfrontend.com', // overwrite
'Access-Control-Allow-Credentials': 'true'
}
})
For other options, refer this link.
Return a restfun
instance with the following methods:
listen(port, host, callback)
: start listening at the specified portget(pattern, handler)
: routes GET request to the specified patternpost(pattern, handler)
: routes POST request to the specified patternput(pattern, handler)
: routes PUT request to the specified patterndelete(pattern, handler)
: routes DELETE request to the specified patternroute(METHOD, pattern, handler
: another way to add router using any HTTP methodsuse(handler)
: insert a handler in the middle of request/response processing, before the router handlersnotFound(handler)
: add handler to deal with 404 erroronError(handler)
: add handler to deal with other errors
This lib only support simple pattern, e.g,:
/:category/:slug
/profile/:userid/
/accounts/:userid/settings
/search
/
A function that accepts an IncomingMessage (a.k.a req
) and a ServerResponse (a.k.a res
).
Along with what are inherited from their prototype, restfun
adds the following properties and methods to req
/res
:
req.ip
req.params
req.query
req.body
req.getHeader()
res.type()
res.status()
res.json()
res.html()
res.send()
autocannon -c 100 -w 4 -d 20 http://0.0.0.0:3001
- Intel® Core™ i7-10510U CPU @ 1.80GHz × 8
- RAM DDR4 2667 MT/s 16GB
- Node.js v18.12.1 on Debian 11.6
git clone https://github.com/ndaidong/restfun.git
cd restfun
pnpm i
pnpm test
The MIT License (MIT)