Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/alinejati-bu/patuervet
https://github.com/alinejati-bu/patuervet
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/alinejati-bu/patuervet
- Owner: aliNejati-bu
- License: mit
- Created: 2024-05-11T12:40:38.000Z (8 months ago)
- Default Branch: master
- Last Pushed: 2024-05-28T15:00:22.000Z (7 months ago)
- Last Synced: 2024-05-29T06:06:04.637Z (7 months ago)
- Language: HTML
- Size: 10.5 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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)
```javascript
import {Controller} from "..";
import {NextFunction, Request, Response} from "express";
import {baseResponse} from "../../helpers/functions";class TwitterController 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.
```javascript
export default function (): TwitterController {
const controller = new TwitterController();
// add method to actions
controller.addAction("/test", "get", controller.testMethod, []);
return controller;
}
```
Finally, routes must be introduced to express somewhere.```javascript
/**
/src/Router/api/v1/index.ts
*/
import {Router} from "express";
import TwitterController from "../../../Controller/Controllers/TwitterController";
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(TwitterController().setupActions());
router.use(AuthController().setupActions())
router.use(UserController().setupActions());
router.use(BusinessController().setupActions());
router.use(OperatorController().setupActions())
export default router;
```