UNPKG

1.98 kBJavaScriptView Raw
1/**
2 * routes/index.js
3 * */
4
5"use strict";
6
7var fs = require('fs'),
8 path = require('path'),
9 exists = require('fs-exists-sync')
10
11module.exports = class {
12
13 constructor(config, paths) {
14 this.config = config;
15 this.paths = paths;
16 this.model = require('../models')(config, paths)
17 this.logger = require('../logs')(config, paths);
18 this.view = new (require('../views'))({config, paths});
19 this.controller = new (require('../controllers'))(config, paths);
20
21 this.controller.view = this.view;
22 this.controller.model = this.model;
23 this.controller.logger = this.logger;
24 }
25
26 loadFiles() {
27 var files = [];
28
29 if (exists(paths.app.routes)) {
30 fs.readdirSync(paths.app.routes)
31 .filter(function (file) {
32 return (file.indexOf(".") !== 0 && path.extname(file) == ".js");
33 })
34 .forEach(function (file) {
35 files.push(path.join(paths.app.routes, file));
36 });
37 }
38
39 return files;
40 }
41
42 auto(express) {
43 var express = require('express'),
44 router = express.Router();
45
46 var files = this.loadFiles();
47
48 this.controller.load();
49
50 for (var key in files) {
51 var appRouter = require(files[key])({
52 'router': express.Router(),
53 'controller': this.controller,
54 'model': this.model,
55 'view': this.view,
56 'paths': this.paths,
57 'config': this.config,
58 'logger': this.logger
59 });
60
61 if (typeof appRouter === 'function') {
62 router.use('/', appRouter);
63 }
64 }
65
66 if(this.config.scaffold !== false){
67 router.all('*', (request, response) => {
68 this.controller.http(request, response).find()
69 });
70 }
71
72 return router;
73 }
74}