Code Monkey home page Code Monkey logo

express-typescript-starter's Introduction

Starter project for Express and TypeScript

This project is a ready structure for developing server projects in nodejs environment with typescript language and express framework.

Routing

In this project, Routing is done in the same controller layer.

Controllers are defined as a contract in the src/Controller/Controllers folder. All controllers inherit from the parent controller, and their constructors specify the basic routing when they call the parent constructor (as a parent constructor parameter)

import {Controller} from "..";
import {NextFunction, Request, Response} from "express";  
import {baseResponse} from "../../helpers/functions";

class ExampleController extends Controller {  
    constructor() {  
	    // base router
        super("/test");  
  }  
  
    async testMethod(req: Request, res: Response, next?: NextFunction) {  
        return baseResponse(res, {}, "validation test", undefined, "ok", 200);  
  }  
}

The controller file must include an instance of the controller class as export default with the help of an anonymous function.

The routes are introduced to the controller as actions in this anonymous function.

export default function (): ExampleController {  
    const controller = new ExampleController();  
  
  // add method to actions  
  controller.addAction("/test", "get", controller.testMethod, []);  
  
 return controller;  
}

Finally, routes must be introduced to express somewhere.

/**
/src/Router/api/v1/index.ts
*/
import {Router} from "express";  
import ExampleController from "../../../Controller/Controllers/ExampleController";  
import AuthController from "../../../Controller/Controllers/AuthController";  
import UserController from "../../../Controller/Controllers/UserController";  
import BusinessController from "../../../Controller/Controllers/BusinessController";  
import OperatorController from "../../../Controller/Controllers/OperatorController";  
  
  
const router = Router();  
  
router.use(ExampleController().setupActions());  
router.use(AuthController().setupActions())  
router.use(UserController().setupActions());  
router.use(BusinessController().setupActions());  
router.use(OperatorController().setupActions())  
export default router;

express-typescript-starter's People

Contributors

alinejati-bu avatar abolfazl8131 avatar

Stargazers

 avatar  avatar Mohsen Abdolahi avatar Behzad Khalifeh avatar mohammad rezaeeian avatar  avatar  avatar AmirHosein Zare avatar

Watchers

 avatar

express-typescript-starter's Issues

calling next when exception throwed

روتر اکسپرس هنوز پرامیس رو ساپورت نمیکنه. اگه متد های async کنترلرهات اکسپشن بدن توی نسخه های قبلی node ریسپانسی نمیگیری، جدید تر ها هم اپ کرش میکنه بخاطر uncatched promise. چند تا راه حل داره سرچ کنی

router.get(action.path, action.middlewares ?? [], action.handler);

+‌کانفیگ هات رو همه بذار توی یک فایل، بعدا نخوای دنبالشون بگردی. و اگه خواستی علاوه بر env از جای دیگه بخونی راحت باشه

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.