3.3.0 β€’ Published 1 month ago

routup v3.3.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 month ago

Routup banner

Routup πŸ§™β€

npm version main codecov Known Vulnerabilities Conventional Commits

Routup is a fast, lightweight, runtime agnostic and asynchronous routing framework. Helpers provide additional functionalities to interact with the request and manipulate the response.

It can be used independently of the selected runtime environment (Node.Js, Bun, ... ) πŸŽ‰. Moreover, it is even 228% faster than Express (more).

Table of Contents

Installation

npm install routup --save

Features

  • πŸš€ runtime agnostic (Node.JS, Bun, Deno, ...)
  • πŸ“ different handler types (base & error)
  • ✨ promise (async) support for core- & error-handlers
  • πŸ“Œ robust hook system
  • πŸ”Œ powerful plugin system
  • 🧰 tree shakeable response & request helpers
  • 🀝️ different handler declaration styles (shorthand & verbose)
  • πŸ“ nestable routers
  • πŸ‘• TypeScript support
  • 🀏 minimalistic to fit into any solution with minimum overhead
  • & much more

Documentation

To read the docs, visit https://routup.net

Usage

The following examples are intended to give a small insight into the use of the framework. However, it is highly recommended to read the documentation, as all concepts and basics are taught there.

Handlers

Both core and error handlers, can be defined in two different ways. Core handler functions can have up to 3 arguments (req, res, next) whereas error handler functions can have up to 4 arguments (err, req, res, next). This should be familiar to anyone who has used express before.

Shorthand

With the shorthand variant, only the handler function is passed as argument to the coreHandler & errorHandler function.

import { createServer } from 'node:http';
import {
    coreHandler,
    createNodeDispatcher,
    errorHandler,
    Router,
    useRequestParam
} from 'routup';

const router = new Router();

router.get('/', coreHandler(() => 'Hello, World!'));
router.get('/greet/:name', coreHandler((req) => `Hello, ${useRequestParam(req, 'name')}!`));
router.use(errorHandler((err) => `An error with statusCode ${err.statusCode} occured.`));

const server = createServer(createNodeDispatcher(router));
server.listen(3000)

Verbose

The verbose variant is more complex, but offers the possibility to set additional information like path, method, ... in the handler definition.

import { createServer } from 'node:http';
import {
    coreHandler,
    createNodeDispatcher,
    errorHandler,
    Router,
    useRequestParam
} from 'routup';

const router = new Router();

router.get(coreHandler({
    path: '/',
    fn: () => 'Hello, World!',
}));

router.get(coreHandler({
    path: '/greet/:name',
    fn: (req) => `Hello, ${useRequestParam(req, 'name')}!`
}))

router.use(errorHandler({
    fn: (err) => `An error with statusCode ${err.statusCode} occured.`
}))

const server = createServer(createNodeDispatcher(router));
server.listen(3000)

Runtimes

It is possible to use any javascript runtime environment. Below are examples for Bun and Deno. These use the web dispatcher to submit requests based on the web api. Besides the node- & web-dispatcher, there is also a plain dispatcher that underlies the web dispatcher, which can be controlled via a simple API.

Bun

import {
    coreHandler,
    createWebDispatcher,
    Router
} from 'routup';

const router = new Router();

router.get('/', coreHandler(() => 'Hello, World!'));

const dispatch = createWebDispatcher(router);

Bun.serve({
    async fetch(request) {
        return dispatch(request);
    },
    port: 3000,
});

Deno

import {
    coreHandler,
    createWebDispatcher,
    Router
} from 'routup';

const router = new Router();

router.get('/', coreHandler(() => 'Hello, World!'));

const dispatch = createWebDispatcher(router);

const server = Deno.listen({
    port: 3000
});
for await (const conn of server) {
    const httpConn = Deno.serveHttp(conn);

    for await (const requestEvent of httpConn) {
        const response = await dispatch(
            requestEvent.request
        );
        requestEvent.respondWith(response);
    }
}

Plugins

According to the fact that routup is a minimalistic framework, it depends on plugins to cover some typically http framework functions, which are not integrated in the main package.

NameDescription
assetsServe static files from a directory.
basicBundle of the body, cookie and query plugin.
bodyRead and parse the request body.
cookieRead and parse request cookies and serialize cookies for the response.
decoratorsCreate request handlers with class-, method- & parameter-decorators.
prometheusCollect and serve metrics for prometheus.
queryRead and parse the query string of the request url.
rate-limitRate limit incoming requests.
rate-limit-redisRedis adapter for the rate-limit plugin.
swaggerServe generated docs from URL or based on a JSON file.

Benchmarks

  • CPUs: 24
  • RAM: 63.9GB
  • Node: v18.16.0
  • Date: Wed Sep 13 2023 15:11:58 GMT+0200 (MitteleuropΓ€ische Sommerzeit)
  • Method: autocannon -c 100 -d 40 -p 10 localhost:3000 (two rounds; one to warm-up, one to measure)
PackageRequests/sLatency (ms)Throughput/MB
http6106215.8710.89
fastify5967916.2610.70
koa4576321.358.16
routup4458821.919.02
hapi4137423.677.38
express1337674.182.39

Benchmarks were generated using autocannon. To recreate the results, this can be done using the benchmarks' repository.

Contributing

Before starting to work on a pull request, it is important to review the guidelines for contributing and the code of conduct. These guidelines will help to ensure that contributions are made effectively and are accepted.

License

Made with πŸ’š

Published under MIT License.

3.3.0

1 month ago

3.2.0

6 months ago

3.1.0

7 months ago

3.0.0-alpha.1

7 months ago

3.0.0-alpha.3

7 months ago

3.0.0-alpha.2

7 months ago

3.0.0

7 months ago

1.0.2

10 months ago

1.0.1

12 months ago

1.0.3

8 months ago

2.0.0

8 months ago

1.0.0

12 months ago

1.0.0-alpha.6

12 months ago

1.0.0-alpha.5

12 months ago

1.0.0-alpha.4

12 months ago

1.0.0-alpha.3

12 months ago

1.0.0-alpha.2

12 months ago

1.0.0-alpha.1

12 months ago

0.11.0

1 year ago

0.12.0

1 year ago

0.13.0

1 year ago

0.14.0

1 year ago

0.13.1

1 year ago

0.14.1

1 year ago

0.14.2

1 year ago

0.10.0

1 year ago

0.9.1

1 year ago

0.9.0

1 year ago

0.8.3

1 year ago

0.8.2

1 year ago

0.8.1

1 year ago

0.8.0

1 year ago

0.7.0

1 year ago

0.6.0

1 year ago

0.5.0

1 year ago

0.4.1

1 year ago

0.4.0

1 year ago

0.3.2

1 year ago

0.3.1

1 year ago

0.3.0

1 year ago

0.3.0-alpha.0

1 year ago

0.2.0

2 years ago

0.1.4

2 years ago

0.1.3

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago

0.0.1

2 years ago