123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- const express = require('express');
- const path = require('path');
- function _fail(req,res) {
- res.sendStatus(500);
- }
- function sendFile(req,res,next, file) {
- let p = path.join(__dirname + '/../public' + file);
- return res.sendFile(p);
- }
- function _setupRoutes(routes, router) {
- for(let i in routes){
- let fn = _fail;
- switch(routes[i].type) {
- case 'file':
- fn = (r,w,n) => sendFile(r,w,n,routes[i].file);
- break;
- case 'custom':
- fn = routes[i].customFn;
- break;
- }
- router[routes[i].method]([routes[i].endpoint], fn);
- }
- }
- class RoutingSystem {
- constructor() {
- this.currentRoutes = [];
- this.router = express.Router();
- }
- addRoute(route) {
- this.currentRoutes.push(route);
- }
- loadRoutes(routes, router, prefix = '/', middlewares = []) {
- if(router === undefined)
- router = express.Router();
- _setupRoutes(routes, router);
- this.router.use(prefix, middlewares, router);
- return router;
- }
-
- }
- module.exports = new RoutingSystem();
|