0.10.1 โ€ข Published 2 years ago

@macchiatojs/router v0.10.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

@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.

ParamTypeDescription
optionsObject
options.prefixStringprefix router paths
options.expressifyBooleanuse express/connect style when is true and koa style when is false (default to true)
options.rawBooleanuse raw Node.js server when is true (default to false)
options.trekBooleanuse 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