Giter Site home page Giter Site logo

srvem / router Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 33 KB

A servem middleware used to develop routers and server APIs with asynchronous request handlers.

License: MIT License

TypeScript 100.00%
servem middleware route router api rest restful server typescript javascript node srvrouter srv async await promise asynchronous

router's Introduction

@srvem/router

A Srvem middleware used to develop routers and server APIs with asynchronous request handlers.

Installation

npm install --save @srvem @srvem/router

Example

import { Context, Srvem } from '@srvem/app'
import { SrvRouter } from '@srvem/router'

// create a Srvem app
const app: Srvem = new Srvem()

// create a SrvRouter and define its routes
const router1: SrvRouter = new SrvRouter()

// GET /greet
router1.get('/greet', async (ctx: Context, next: () => Promise<void>): Promise<void> => {
  ctx.statusCode = 200
  ctx.body = 'Hello, world!'
})

// POST /sample/path/to/somewhere
router1.post('/sample/path/to/somewhere',
  async (ctx: Context, next: () => Promise<void>): Promise<void> => {
    // some code, e.g. route handling response time logger (using `await next()`)
  },
  async (ctx: Context, next: () => Promise<void>): Promise<void> => {
    // more code, e.g. authentication and authorization
  },
  async (ctx: Context, next: () => Promise<void>): Promise<void> => {
    // even more, e.g. actual data response
  }
)

// use router1
app.use(router1)

// listen on port 3000
app.server.listen(3000)

API

import { Context, MiddlewareBlueprint } from '@srvem/app'

import { RouteHandlerType } from './IRoute'



/**
 * Used to develop routers and server APIs with asynchronous request handlers.
 */
declare class SrvRouter extends MiddlewareBlueprint {
  /**
   * Constructs the SrvRouter middleware.
   */
  constructor()

  /**
   * Filters routes by the requests method and URL, then fires thier respective handlers.
   *
   * @param ctx The Context
   */
  main(ctx: Context): Promise<void>

  /**
   * Adds a route and its handler(s).
   *
   * @param method Request method
   * @param path Request path
   * @param handlers Route handler(s)
   */
  addRoute(method: string, path: string, ...handlers: RouteHandlerType[]): void

  /**
   * Adds a route and its handler(s) with nil method.
   *
   * @param path Request path.
   * @param handlers Route handler(s)
   */
  all(path: string, ...handlers: RouteHandlerType[]): void

  /**
   * Adds a route and its handler(s) with GET method.
   *
   * @param path Request path.
   * @param handlers Route handler(s)
   */
  get(path: string, ...handlers: RouteHandlerType[]): void

  /**
   * Adds a route and its handler(s) with POST method.
   *
   * @param path Request path.
   * @param handlers Route handler(s)
   */
  post(path: string, ...handlers: RouteHandlerType[]): void

  /**
   * Adds a route and its handler(s) with PUT method.
   *
   * @param path Request path.
   * @param handlers Route handler(s)
   */
  put(path: string, ...handlers: RouteHandlerType[]): void

  /**
   * Adds a route and its handler(s) with DELETE method.
   *
   * @param path Request path.
   * @param handlers Route handler(s)
   */
  del(path: string, ...handlers: RouteHandlerType[]): void
}



/**
 * Route handler type.
 */
declare type RouteHandlerType = (ctx: Context, next: () => Promise<void>) => Promise<void>;

/**
 * Route type (with its handler(s)).
 */
interface IRoute {
  method: string;
  path: string;
  handlers: RouteHandlerType[];
}

See Also

  • @srvem/app - The core package of Srvem (contains a class used to construct a Srvem app).
  • @srvem/static - A Srvem middleware used to serve static files from a specified directory.

Credits

Kaleab S. Melkie <[email protected]>

License

MIT License
Copyright © 2017 srvem

Made with ❤ in Addis Ababa.

router's People

Contributors

kaleabmelkie avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.