npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

restfun

v0.0.15

Published

Minimal and fast rest api on the edges

Downloads

5

Readme

restfun

To run minimal and fast rest api on the edges.

npm CodeQL CI test Coverage Status

Install

pnpm i restfun
bun add restfun

Usage

Node.js

Hello word with restfun:

import restfun from 'restfun'

const server = restfun()

server.get('/', (req, res) => {
  res.html('Hello world')
})

server.listen(3001)

APIs

restfun

Syntax

restfun()
restfun(Object options)

Parameters

options optional
  • cors: object, headers for cors, default to {}
  • noDelay: bolean, default to true
  • keepAlive: bolean, default to false
  • maxHeaderSize: number, default to 16384
  • headersTimeout: number, default to 60000
  • requestTimeout: number, default to 300000

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

Return a restfun instance with the following methods:

  • listen(port, host, callback): start listening at the specified port
  • get(pattern, handler): routes GET request to the specified pattern
  • post(pattern, handler): routes POST request to the specified pattern
  • put(pattern, handler): routes PUT request to the specified pattern
  • delete(pattern, handler): routes DELETE request to the specified pattern
  • route(METHOD, pattern, handler: another way to add router using any HTTP methods
  • use(handler): insert a handler in the middle of request/response processing, before the router handlers
  • notFound(handler): add handler to deal with 404 error
  • onError(handler): add handler to deal with other errors
patterns

This lib only support simple pattern, e.g,:

  • /:category/:slug
  • /profile/:userid/
  • /accounts/:userid/settings
  • /search
  • /
handler

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()

Benchmark

autocannon -c 100 -w 4 -d 20 http://0.0.0.0:3001

Benchmark

  • Intel® Core™ i7-10510U CPU @ 1.80GHz × 8
  • RAM DDR4 2667 MT/s 16GB
  • Node.js v18.12.1 on Debian 11.6

Test

git clone https://github.com/ndaidong/restfun.git
cd restfun
pnpm i
pnpm test

License

The MIT License (MIT)