Skip to content

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

License

Notifications You must be signed in to change notification settings

macchiatojs/router

Repository files navigation

@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

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published