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

express-ts-decorator

v0.1.21

Published

Express typescript descorators

Downloads

7

Readme

express-ts-decorator

The Express typescript easy to use decorator project

Install

  # use npm
  ~ npm i express-ts-decorator
  # use yarn
  ~ yarn add express-ts-decorator

Usage

  1. Set up your Controller
// ./controllers/hello.ts
import { Controller, Get } from "express-ts-descorator";

@Controller("/api/hello")
export class Hello {
  @Get("")
  public hello() {
    return "hello";
  }

  @Get("/json")
  public jsonHello() {
    return {
      message: "hello"
    };
  }
}
  1. Set up your App
// app.ts
import * as express from "express";
import { ControllerLoader, ExpressApp } from "express-ts-descroator";
import * as Path from "path";
import * as cors from "cors";

@ControllerLoader({
  filePath: Path.join(__dirname, "./controllers") // path to your controllers file
  autoInjectRoutes: true // auto inject router config to express when App constructor
})
class App extends ExpressApp {

  // if use auto inject routes
  // some middlewares can set in this array which you want it run before router
  beforeRouterInjectMiddlewares = [
    (req: any, res: any, next: any) => {
      console.log("in before router middleware");
      return next();
    },
    bodyParser.json(),
    {
      active: () => process.env.NODE_ENV === "DEVELOPMENT", // if some middleware just run in production or development you can use this
      middleware: cors()
    }
  ];

  constructor() {
    super(express());
  }
}

const app = new App();

// use the middlewares like that
// app.use(bodyParser.json());

// inject the loader routes config
// if you set the `autoInjectRoutes` is false in ControllerLoader,
// you can inject router by yourself like that
app.routes.forEach(router => {
  app.use(router);
});

// catch the error like that
app.use((err: Error, req: express.Request, res: express.Response, next: express.NextFunction) => {
  // do sth here
});

app.express.listen(3000);
  1. Run!
  ~ ts-node app.ts

Test

  ~ yarn test

Validator

you can use the Joi to validate your http request parameter

// ./controllers/hello.ts
import { Controller, Get, Validator } from "express-ts-descorator";
import * as Joi from "@hapi/joi";

// support the `query`, `body`, `params` validator in http request.
@Validator({
  query: Joi.object()
    .keys({
      name: Joi.string().required() // will throw error when request.query.name not exist or not the string
    })
    .requried()
  // body: .....
  // params: .....
})
@Controller("/api/hello")
export class Hello {
  @Get("")
  public hello() {
    return "hello";
  }

  @Get("/json")
  public jsonHello() {
    return {
      message: "hello"
    };
  }
}

Middlewares

import { Controller, Get, Middlewares, Validator } from "express-ts-decorator";
import * as Joi from "@hapi/joi";
import { Request, Response, NextFunction } from "express";

@Controller("/api/hello")
export class Hello {
  @Validator({
    query: Joi.object()
      .keys({
        name: Joi.string().required()
      })
      .required()
  })
  @Middlewares(function(req: Request, res: Response, next: NextFunction) {
    console.log("in");
    // You can do something in here before call the controller main function
    next();
  })
  @Get("")
  public hello() {
    return "hello";
  }

  @Get("/json")
  public jsonHello() {
    return {
      message: "hello"
    };
  }
}