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

expressts-router

v1.1.3

Published

Routing Decorators for Express apps

Downloads

4

Readme

Express TS Router

A simple library which uses powerful Typescript feature - Decorator - to create Routes for express with typescript application

Installation

Using npm/yarn:

$ npm i -s expressts-router
$ yarn add expressts-router

Set these 2 properties in your tsconfig.json

"experimentalDecorators": true
"emitDecoratorMetadata": true

Usage

1. Environment variable Define a path pattern to your controller files by adding 'X_CONTROLLERS_PATH' environment variable in your .env file. If no value specified, the default path pattern will be 'dist/controllers/**/*.controller.js' Note The path pattern must lead to the compiled .js files, not the .ts file

2. Create your controller class: For example, I created a file 'src/controllers/value.controller.ts' In 'value.controller.ts':

import { Controller } from "expressts-router"

// You can specify the prefix to this controller by passing a string to @Controller decorator
// Ex: By passing '/value' to the @Controller decorator, the prefix to this controller will be '{host}/value'
@Controller('/value')
export class ValueController {

}

Define your request handler in 'value.controller.ts':

import { Request, Response, NextFunction } from "express"
import { 
    Controller,
    Get,
    Post
} from "expressts-router"
import { someMiddleware } from '../middlewares'

@Controller('/value')
export class ValueController {
    // Use @Get decorator to define a GET request handler
    @Get({ path: '' })
    getValues(req: Request, res: Response, next: NextFunction) {
        res.json(['value1', 'value2'])
    }

    // There are other decorators for POST, PUT, PATCH, DELETE methods as well
    @Post({ path: '' })
    createValue(req: Request, res: Response, next: NextFunction) {
        // TODO: Create value
    }

    // You can also use middlewares
    @Get({
        path: '/with-middleware',
        middlewares: [ someMiddleware ]
    })
    getValuesWithMiddlewares(req: Request, res: Response, next: NextFunction) {
        res.json(['value1', 'value2'])
    }
}

In your root file (usually index.ts or app.ts):

  • If you want to use async/await:
import express from 'express'
import { createRoutes } from 'expressts-router'
const app = express()

const startApp = async () => {

    app.use(await createRoutes())

    app.listen(3000, () => {
        console.info(`Server is running on port 3000`)
    })
}
startApp()
  • If you want to use promise:
import express from 'express'
import { createRoutes } from 'expressts-router'
const app = express()

// create routes and return the router
createRoutes().then(router => {
    // use created router
    app.use(router)
})

app.listen(3000, () => {
    console.info(`Server is running on port 3000`)
})
startApp()

That's all!! Open http://localhost:3000/value on your browser The response should be

['value1', 'value2']

Exports

createRoutes(router: Router): Router

Create routes and return the router Parameters: - router?: Router object created by Router() function from 'express'. If router is undefined, create new Router Returns: - router: Router object which has routes defined

Controller(prefix: string)

Define a controller Parameters: - prefix: string - the url prefix to access to the controller, default to ''

Get(httpActionOptions: HTTPActionOptions)

Define a GET request handler

Post(httpActionOptions: HTTPActionOptions)

Define a POST request handler

Patch(httpActionOptions: HTTPActionOptions)

Define a PATCH request handler

Put(httpActionOptions: HTTPActionOptions)

Define a PUT request handler

Delete(httpActionOptions: HTTPActionOptions)

Define a DELETE request handler

HTTPActionOptions:

  • path: string
  • middlewares?: RequestHandler[] - an array of Express's request handlers

Change logs

1.1.0: Add new export getControllers