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-ez-ts-router

v1.1.5

Published

Easy to use Typescript decorators for creating Express.js routes

Downloads

12

Readme

Version   Typescript   Build   License  

Easy to use Typescript decorators for creating Express.js routes

To use express-ez-ts-router in your project you need the configurations below in your tsconfig.json file

{
  "compilerOptions": {
    "target": "es5",
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true
  }
}

Examples:

import express from "express";
import { Router } from "express-ez-ts-router";

const app = express();

// Attach express-ez-ts-router router to express
app.use(Router.getRouter());

app.listen(3000, () => {
    console.log('Listening on port 3000!!!');
})
// Create your first controller
import { Request, Response } from "express";
import { Get, Controller } from "express-ez-ts-router";

@Controller("/main")
class MainController {
    @Get("/success")
    successRoute(request: Request, response: Response) {
        response.json({ success: true });
    }
}
// Chain routes
import { Request, Response } from "express";
import { Post, Controller, SubRouter, UseSubRouter } from "express-ez-ts-router";

@SubRouter('/subroute2')
class SubRouter2 {
    @Post('/login')
    Login(req: Request, res: Response) {
        res.status(200).json({});
    }
}

@SubRouter('/subroute1')
@UseSubRouter(SubRouter2)
class SubRouter1 {}

@Controller('/api')
@UseSubRouter(SubRouter1)
class MainController {}

// POST: /api/subroute1/subroute2/login
// Attach middlewares to methods
import { Request, Response, NextFunction } from "express";
import { Get, Controller, Use } from "express-ez-ts-router";

const protectedRoute = (req: Request, res: Response, next: NextFunction) => {
    res.status(401).json({});
};

const anotherMiddleware = (req: Request, res: Response, next: NextFunction) => {
  // Do stuff
  next();
};

@Controller('/main')
class MainController {
    @Get('/success')
    @Use(protectedRoute) // Attach protectedRoute middleware to this route
    @Use(anotherMiddleware) // Attach anotherMiddleware middleware to this route
    // Multiple middlewares can be attached to the same object(controller, subrouter, request handler). Execution of order is from bottom to top. In this case anotherMiddleware will be executed before protectedRoute
    Success(req: Request, res: Response) {
        res.status(200).json({});
    }
}
// Attach middlewares to controllers
import { Request, Response, NextFunction } from "express";
import { Get, Controller, Use, SubRouter, UseSubRouter } from "express-ez-ts-router";

const notAuthorized = (req: Request, res: Response, next: NextFunction) => {
  res.status(401).json({});
};

@SubRouter('/subroute3')
class SubRoute3 {
  @Get('/user')
  User(req: Request, res: Response) {
    res.status(200).json({});
  }
}

@SubRouter('/subroute2')
class SubRoute2 {
  @Get('/user')
  User(req: Request, res: Response) {
    res.status(200).json({});
  }
}

@SubRouter('/subroute')
@UseSubRouter(SubRoute2)
class SubRoute {
  @Get('/user')
  User(req: Request, res: Response) {
    res.status(200).json({});
  }
}

@Controller('/main')
@UseSubRouter(SubRoute)
@UseSubRouter(SubRoute3)
@Use(notAuthorized)
class MainController {
  @Get('/notfound')
  NotFound(req: Request, res: Response) {
    res.status(200).json({});
  }
}

/*
/main/notfound
/main/subroute/user
/main/subroute3/user
/main/subroute/subroute2/user
All defined routes will return status code 401 because the main controller uses the middleware notAuthorized
 */
// Attach middlewares to sub routers
import { Request, Response, NextFunction } from "express";
import { Get, Controller, Use, SubRouter, UseSubRouter } from "express-ez-ts-router";

const notAuthorized = (req: Request, res: Response, next: NextFunction) => {
    res.status(401).json({});
};

@SubRouter('/subroute3')
class SubRouter3 {
  @Post('/login')
  Login(req: Request, res: Response) {
    res.status(200).json({});
  }
}

@SubRouter('/subroute2')
class SubRouter2 {
  @Post('/login')
  Login(req: Request, res: Response) {
    res.status(200).json({});
  }
}

@SubRouter('/subroute1')
@UseSubRouter(SubRouter2)
@Use(notAuthorized)
class SubRouter1 {
  @Post('/login')
  Login(req: Request, res: Response) {
    res.status(200).json({});
  }
}

@Controller('/api')
@UseSubRouter(SubRouter1)
@UseSubRouter(SubRouter3)
class MainController {
  @Post('/login')
  Login(req: Request, res: Response) {
    res.status(200).json({});
  }
}
/*
/api/login expected status 200
/api/subroute3/login expected status 200
/api/subroute1/login expected status 401
/api/subroute1/subroute2/login expected status 401
Only the routes inside SubRouter1 and SubRouter2 will return with status 401
*/

| Decorators | Description | |:-------------------------------------------------------------------------------:|:-----------------------------------------------------------------------------------------------------------------------------------------------------:| | Controller | Create a main controller with a main route | | SubRouter | Create a sub router which can be attached to another sub router or controller | | UseSubRouter | Attach a sub router to a controller or to another sub router, multiple sub routers can be attached to a single controller or sub router | | Use | Attach middleware function to a controller, sub router or a specific request handler, multiple middlewares can be attached to an object(see examples) | | Method Decorators: Get, Post, Put, Delete, Patch, Head, Options, Connect, Trace | Turn class method of a controller or sub router into a request handler with one of the http request methods |