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

@themost/router

v1.1.0

Published

Router services for express.js applications

Downloads

2

Readme

npm Dependency status for latest release GitHub top language License GitHub last commit GitHub Release Date npm

MOST Web Framework Logo

@themost/router

Router service for express.js

Install

npm i @themost/router

Usage

RouterService service provides a different approach for handling requests by using controllers. A controller is an instance of HttpController class which uses javascript decorators for defining extra attributes in both classes and methods.

import { HttpController, httpController, httpGet } from '@themost/router';

@httpController()
export class IndexController extends BaseController {

    @httpGet()
    index() {
        return this.json({
            message: 'Hello World!'
        });
    }

}

IndexController is marked by @httpController class decorator and IndexController.index() by @httpGet method decorator.

Create an instance of HttpApplication class and register controllerRouter express.js middleware:

const app = express();
app.use(express.json());
...
const httpApp = new HttpApplication();
const routes = [
    {
        path: '',
        action: 'index',
        controller: IndexController
    },
    ...
];
httpApp.getService(RouterService).addRange(...routes);
app.use(controllerRouter(httpApp));

@httpController()

Annotates a class which is going to be used as controller

@httpGet(name,params)

A method decorator which defines an HTTP GET method

name: string

A string which represents the name of the method while executing HTTP requests. This name is being used by routing service while parsing routes which contain :action param:

{
    path: 'home',
    controller: HelloController,
    children: [
        {
            path: '',
            redirectTo: 'index'
        },
        {
            path: ':action'
        }
    ]
}

params : HttpParamAttributeOptions[]

An array of objects which represents a collection of route or querystring params

@httpGet({
    params: [
        {
            name: 'id',
            type: 'Integer',
            required: true
        }
    ]
})
getItem(id) {
    //
}

@httpPost(name,params)

A method decorator which defines an HTTP POST method

@httpPost({
    name: 'reply',
    params: [
        {
            name: 'message',
            type: 'Text',
            maxLength: 512
        }
    ]
})
reply(message) {
    //
}

@httpPut(name,params)

A method decorator which defines an HTTP PUT method

name: string

A string which represents the name of the method while executing HTTP requests. This name is being used by routing service while parsing routes which contain :action param:

params : HttpParamAttributeOptions[]

// e.g. PUT /api/items

@httpPut({
    params: [
        {
            name: 'item',
            fromBody: true,
            required: true
        }
    ]
})
update(item) {
    //
}

An array of objects which represents a collection of route or querystring params

@httpDelete(name,params)

A method decorator which defines an HTTP DELETE method

name: string

A string which represents the name of the method while executing HTTP requests. This name is being used by routing service while parsing routes which contain :action param:

params : HttpParamAttributeOptions[]

An array of objects which represents a collection of route or querystring params

// e.g. DELETE /api/items/:id

@httpDelete({
    params: [
        {
            name: 'id',
            required: true
        }
    ]
})
remove(id) {
    // remove item by id
}

@httpPatch(name,params)

A method decorator which defines an HTTP PATCH method

@httpHead(name,params)

A method decorator which defines an HTTP HEAD method

@httpOptions(name,params)

A method decorator which defines an HTTP OPTIONS method

@httpActionConsumer(HttpConsumer | ConsumerFunction)

A method decorator which defines an operation that is going to be executed before processing controller action.

// GET /api/home/messages

@httpGet({
    name: 'messages'
})
@httpActionConsumer(async (context) => {
    const username = (context.user && context.user.name) || 'anonymous';
    if (username === 'anonymous') {
        throw new AccessDeniedError();
    }
})
getMessages() {
    return [
        {
            message: 'Hello World'
        }
    ];
}    

e.g. a consumer which validates if context.user is empty and throws access denied error.