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

@dlvlup/controllerbuilder

v1.1.2

Published

This package contains decorators that can be used in an Express application. These decorators allow you to easily define and mount HTTP routes for your Express server.

Downloads

1

Readme

Controller Builder

This package contains decorators that can be used in an Express application. These decorators allow you to easily define and mount HTTP routes for your Express server.

Installation

To use this module, you must first install it using npm:

# using npm
npm install @dlvlup/controllerbuilder

# using yarn
yarn add @dlvlup/controllerbuilder

# using pnpm
pnpm add @dlvlup/controllerbuilder

Dependencies

  • This module requires the express package to be installed and imported in your code.
  • This module requires the reflect-metadata package to be installed and imported in your code.

Usage

This module exports three decorators that can be used to define HTTP routes in your Express application: @HttpRoute, @Get, and @Post. This decorators can be used on a class that extends BaseController class which is exported also from the package. That way we can define a controller like so:

HttpRoute

The @HttpRoute decorator is a class decorator that allows you to define HTTP routes for your Express server. This decorator takes a string argument that specifies the name of the route.

Only one of this should be declared on top of the class that will extend BaseController

import {BaseController} from "@dlvlup/controllerbuilder";
import {HttpRoute} from "@dlvlup/controllerbuilder/lib/decorators";

@HttpRoute('example-name')
class ExampleController extends BaseController {
    // ...
}

Get

The @Get decorator is a method decorator that allows you to define a GET route for your Express server. This decorator takes a string argument that specifies the endpoint of the route.

import {BaseController} from "@dlvlup/controllerbuilder";
import {Get, HttpRoute} from "@dlvlup/controllerbuilder/lib/decorators";
import {Request, Response, NextFunction} from 'express';

@HttpRoute('example-name')
class ExampleController extends BaseController {

    @Get('/exampleGetRoute')
    async exampleGetFunction(req: Request, res: Response, next: NextFunction) {
        // ...
    }
}

Post

The @Post decorator is a method decorator that allows you to define a POST route for your Express server. This decorator takes a string argument that specifies the endpoint of the route.

import {BaseController} from "@dlvlup/controllerbuilder";
import {HttpRoute, Post} from "@dlvlup/controllerbuilder/lib/decorators";
import {Request, Response, NextFunction} from 'express';

@HttpRoute('example-name')
class ExampleController extends BaseController {

    @Post('/examplePostRoute')
    async examplePostFunction(req: Request, res: Response, next: NextFunction) {
        // ...
    }
}

Middleware

The @Middleware decorator is a method decorator that allows you to specify middleware for a route. This decorator takes a function argument that executes before the route specific function.

import {BaseController} from "@dlvlup/controllerbuilder";
import {HttpRoute, Post, Middleware} from "@dlvlup/controllerbuilder/lib/decorators";
import {Request, Response, NextFunction} from 'express';

@HttpRoute('example-name')
class ExampleController extends BaseController {

    @Post('/exampleRouteWithMiddleware')
    @Middleware((req: Request, res: Response, next: NextFunction) => {
        // ...code to execute
        next();
    })
    public examplePostFunctionWithMiddleware(req: Request, res: Response) {
        // ...
    }
}

Mounting controllers

To be able to see these routes on your express app we call the constructor of each controller class passing our express app to each. This is what we call "Mounting the controllers".

In your App.ts do something like:

// import your controllers as an default array of modules preferably
import Controllers from './controllers';
import express, {Express} from "express";

const app: Express = express()
    .use(express.json())
    .use(cors());

Controllers.forEach(controller => new controller(app));

Now all the routes will be "mounted" on your express app and accessible when your app start listening.

Notes

  • These decorators are not meant to be used as regular functions.