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

@macchiatojs/router

v0.10.1

Published

Expressive elegant modern amiable Router for raw Node.js/Macchiato.js/Koa.js ⚡.

Downloads

24

Readme

@macchiatojs/router


Build Status Coverage Status NPM version Code Size License

Expressive elegant modern amiable glamorous Macchiato.js Router ⚡ (support also raw Node.js and Koa.js) .

Features

  • 🦄 Based on top of Trouter and/or Trek Router.
  • 🚀 Isomorphic to the moon.
  • 💅🏻 Express-style routing (_.get, _.post, _.put, _.patch, _.delete, etc.)
  • 🔥 Blaze and lightweight router.
  • ⚖️ Tiny Bundle.
  • 🪁 Named URL parameters.
  • 🎯 Route middleware.
  • 🥞 Support router layer middlewares.
  • 📋 Responds to OPTIONS requests with allowed methods.
  • ⛔️ Support for 405 Method Not Allowed.
  • ❌ Support for 501 Path Not Implemented.
  • 🧼 Support trailing slash and fixed path by automatic redirection.
  • ✨ Asynchronous support (async/await).
  • 🐱‍👤 Support Koa.js and all framework which have the same behave.
  • 🐢 Raw Node.js (http) support.
  • 🎉 TypeScript support.

Installation

# npm
$ npm install @macchiatojs/router
# yarn
$ yarn add @macchiatojs/router

Usage

This is a practical example of how to use.

import Macchiato, { Request, Response } from "@macchiatojs/kernel";
import Router from "@macchiatojs/router";

const app = new Macchiato();
const router = new Router(); // use trouter
// >>> some benchs say that trek-router have better perf than trouter. <<< //
// const router = new Router({ trek: true }); // use trek-router

router.get("/hello", (request: Request, response: Response) => {
  response.body = "Hello World";
});

app.use(router.routes());

app.start(2222);

with raw Node.js

import http, { IncomingMessage, ServerResponse } from "http";
import Router from "@macchiatojs/router";

const router = new Router({ raw: true });

router.get("/hello", (request: IncomingMessage, response: ServerResponse) => {
  response.statusCode = 200;
  response.write("Hello World !");
  response.end();
});

const server = http.createServer(router.rawRoutes());

server.listen(2222);

with Koa.js

import Koa from "koa";
import Router from "@macchiatojs/router";

const app = new Koa();
const router = new Router<Koa.Middleware>({ expressify: false });

router.get("/hello", (ctx: Koa.BaseContext) => {
  ctx.body = "Hello World !";
});

app.use(router.routes());

app.listen(2222);

API

Note:

We use @macchiatojs/kernel (needed only when use TypeScript and/or Macchiato.js), parseurl (needed only when use raw Node.js), @types/koa (needed only when use TypeScript) and koa (needed only when use Koa.js) as peerDependencies.

new Router(options?)

Create a new router.

| Param | Type | Description | | -------------------- | --------- | ---------------------------------------------------------------------------------------------- | | [options] | Object | | | [options.prefix] | String | prefix router paths | | [options.expressify] | Boolean | use express/connect style when is true and koa style when is false (default to true) | | [options.raw] | Boolean | use raw Node.js server when is true (default to false) | | [options.trek] | Boolean | use trek-router when is true and trouter when is false (default to false) |

router.get|post|put|patch|delete|all(path, handler)

The http methods provide the routing functionality in router.

Method middleware and handlers follow usual raw Node.js and express middleware or koa middleware behavior, except they will only be called when the method and path match the request.

// handle a GET / request.

// raw Node Style
router.get("/", (request, response) => {
  response.statusCode = 200;
  response.write("Hello World !");
  response.end();
});

// Express/Connect Style
router.get("/", (request, response) => {
  response.send(200, "Hello World !");
});

// Koa Style
router.get("/", (ctx) => {
  ctx.response.send(200, "Hello World !");
});

router.prefix(prePath)

Route paths can be prefixed at the router level:

// handle a GET /prePath/users request.

// raw Node Style
router.prefix("/prePath").get("/", (request, response) => {
  response.statusCode = 200;
  response.write("Hello World !");
  response.end();
});

// Express/Connect Style
router.prefix("/prePath").get("/users", (request, response) => {
  response.send(200, "Hello World !");
});

// Koa Style
router.prefix("/prePath").get("/users", (ctx) => {
  ctx.response.send(200, "Hello World !");
});

router.route(path)

Lookup route with given path.

// handle a GET /users request.

// raw Node Style
router.prefix("/users").get((request, response) => {
  response.statusCode = 200;
  response.write("Hello World !");
  response.end();
});

// Express/Connect Style
router.route("/users").get((request, response) => {
  response.send(200, "Hello World !");
});

// Koa Style
router.route("/users").get((ctx) => {
  ctx.response.send(200, "Hello World !");
});

router.use(...middlewares)

Use given middleware(s). Currently, use middleware(s) for all paths of router isntance.

router.routes()

Returns router middleware which handle a route matching the request for Macchiato.js and Koa.js.

router.rawRoutes()

Returns router middleware which handle a route matching the request for raw Node.js.

Support

If you have any problem or suggestion please open an issue.

Related

License


MIT © Imed Jaberi