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

@brarsanmol/express-decorators

v2.7.3

Published

node decorators - decorators for express library

Downloads

7

Readme

Node Decorators

Project implements decorators for modern tools for NodeJS like:

Installation

npm install @decorators/express --save

API

Functions

  • attachControllers(app: Express.Application | Express.Router, controllers: Controller[]) - attach controllers to express application

Decorators

Class
  • @Controller(baseUrl: string, middleware?: Middleware[]) - Registers controller for base url
Method
  • @All(url: string, middleware?: Middleware[]) - Registers all routes
  • @Get(url: string, middleware?: Middleware[]) - Registers get route
  • @Post(url: string, middleware?: Middleware[]) - Registers post route
  • @Put(url: string, middleware?: Middleware[]) - Registers put route
  • @Delete(url: string, middleware?: Middleware[]) - Registers delete route
  • @Patch(url: string, middleware?: Middleware[]) - Registers patch route
  • @Options(url: string, middleware?: Middleware[]) - Registers options route
  • @Head(url: string, middleware?: Middleware[]) - Registers head route

where middleware is the class that implements Middleware interface.

To use class, import Middleware interface and implement it, like so:

import { Middleware } from '@decorators/express';

class UserMiddleware implements Middleware {
  public use(request: Request, response: Response, next: NextFunction): void {
    next();
  }
}
Parameter
  • @Request(property?: string) - Returns express req object or any other object, if name was specified
  • @Response() - Returns express res object
  • @Next() - Returns express next function
  • @Params(param?: string) - Express req.params object or single param, if param name was specified
  • @Query(param?: string) - Express req.query object or single query param, if query param name was specified
  • @Body(param?: string) - Express req.body object or single body param, if body param name was specified
  • @Headers(property?: string) - Express req.headers object or single headers param, if headers param name was specified
  • @Cookies(param?: string) - Express req.body object or single cookies param, if cookies param name was specified

Error middleware

To add error middleware, that handles unhandled errors simply implement ErrorMiddleware interface and provide it using ERROR_MIDDLEWARE token, like so:

import { ErrorMiddleware, ERROR_MIDDLEWARE } from '@decorators/express';

@Injectable()
class ServerErrorMiddleware implements ErrorMiddleware {
  public use(error: Error, request: Request, response: Response, next: NextFunction) {
    next();
  }
}

Container.provide([
  { provide: ERROR_MIDDLEWARE, useClass: ServerErrorMiddleware }
]);

Dependency injection

This module supports dependency injection provided by @decorators/di module. For example, see the full example below.

Example Express Application and Controller:

import {
  Response, Params, Controller, Get,
  attachControllers, Middleware
} from '@decorators/express';

@Controller('/')
class UsersController {

  constructor(private userService: UserService) {}

  @Get('/users/:id')
  getData(@Response() res, @Params('id') id: string) {
    res.send(this.userService.findById(id));
  }
}

let app = express();
attachControllers(app, [UsersController]);

app.listen(3000);

You can also attach controllers to express.Router instance. This is useful when you want to namespace all of the routes with a prefix:

import { attachControllers } from '@decorators/express';

const apiRouter = express.Router();
attachControllers(apiRouter, [UsersController]);

app.use('/v1/api', apiRouter);