1.5.2 • Published 6 months ago

couch-routes v1.5.2

Weekly downloads
-
License
ISC
Repository
github
Last release
6 months ago

couch-routes

What is this?

couch-routes is a simple and light-weight package built to simplify routing and dependencies through a typescript class based approch to express.

Why use couch-routes

The intention of couch-routes is to clean up the common file structure of an express application while making it more intuitive and familliar to the common structure and feel of web frameworks throughout different languages. This packages takes consideration from Dot Net as well as Angular, giving a familliar routing and dependency injection experience.

Installation

npm i couch-routes --save

tsconfig.json

"compilerOptions": {      ...      "experimentalDecorators": true,      "emitDecoratorMetadata": true, }

Usage

app.ts

import * as router from "couch-routes";
import controllers from "./controllers" //Array of controller classes

const app = express();

router.initialize(app, controllers);

Controllers

import * as express from "express";
import { MyService } from "../services/myService"'
import {Controller, Get, Put, Post, Delete } from "couch-routes/decorators";

@Controller("/user")
export default class UserController {

    constructor(private readonly myService: MyService) {}

    @Get("/:id")
    public async getUserById(req: express.Request, res: express.Response) {
        const id = req.query.id;
        res.send(await this.myService.getUserById(id));
    }

    @Get()
    public async getAll(req: express.Request, res: express.Response) {
        res.send(await this.myService.getAll());
    }

    @Delete("/:id")
    public async deleteUser(req: express.Request, res: express.Response) {
        res.send(await this.myService.delete(req.query.id))
    }

    @Post()
    public async CreateUser(req: express.Request, res: express.Response) {
        res.send(await this.myService.add(req.body.user));
    }

    @Put("/:id")
    public async deleteUser(req: express.Request, res: express.Response) {
        res.send(await this.myService.update(req.query.id, req.body.user))
    }

}

Dependency injection for non controllers

import { Service } from "couch-routes/decorators"
import { UserData } from "../data/userData";

@Service()
export default class MyService {

    constructor(private readonly userData: UserData) {

    }

    public async getUserById(id: number) {
        return await this.userData.getUser(id);
    }
}

Options

Controller(routePrefix: string) Get(route: string) Put(route: string) Post(route: string) Delete(route: string)

all of the above default with "/" if nothing is passed

1.5.2

6 months ago

1.5.1

6 months ago

1.5.0

6 months ago

1.4.0

6 months ago

1.3.0

6 months ago

1.2.0

2 years ago

1.2.2

2 years ago

1.2.1

2 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.11

3 years ago

1.0.10

3 years ago

1.0.12

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago