Node-TS-Auth-API-Starter

A template repo for RESTful APIs built with ExpressJS & TypeScript using JWT Authentication


Project maintained by DLMousey Hosted on GitHub Pages — Theme by mattgraham

Routers are responsible for determining if an incoming request matches an endpoint that your API has exposed. Located in src/routes, you will notice that all files in this directory match the pattern of <name>.router.ts, apart from index.ts which is used to make importing routers much easier during application startup.

Registering a new router

import { Router } from "express";
import { ApiRouter } from "./api-router.interface";
import { Controller } from "./../controllers/controller";
import ExampleController from "./../controllers/example.controller";

export default class ExampleRouter implements ApiRouter {
    
    public path = "/api/example";
    public router: Router;
    public controller: ExampleController;
    
    constructor() {
        this.router = Router();
        this.controller = new ExampleController();
        
        this.initRoutes();
    } 

    initRoutes(): void {
        this.router.get("/example", (req, res) => this.controller.getList(req, res));
        this.router.get("/example/:id", (req, res) => this.controller.get(req, res));
        this.router.post("/example", (req, res) => this.controller.create(req, res));
        this.router.put("/example/:id", (req, res) => this.controller.update(req, res));
        this.router.delete("/example/:id", (req, res) => this.controller.delete(req, res));
    }
}
//... Other routers
import ExampleRouter from "./example.router";

export {
    //... Other routers
    ExampleRouter
}

Now that you’ve created and registered a router, you should add a controller to go with it