-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
68 lines (53 loc) · 1.64 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
const express = require('express');
const path = require('path');
const fs=require("fs");
const app = express();
const faker = require('faker');
const config = require("./config.json");
const cors = require('cors');
const low = require('lowdb');
const FileSync = require('lowdb/adapters/FileSync');
//database file
const adapter = new FileSync('db.json');
const db = low(adapter);
// Set some defaults (required if your JSON file is empty)
//db.defaults({ company: {}, customers: {}, count: 0 }).write();
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
//enable cors for all origins
app.use(cors());
//serve static files
app.use(express.static(path.join(__dirname, 'public')));
//activate EJS template engine
app.set('view engine', 'ejs');
//root route
app.get('/', (req, res) => {
// render the `post.ejs` template with the post content
res.render('index', getIndexParams())
});
//Routes
fs.readdirSync('./routes').forEach(function(file) {
const route='./routes/'+file;
require(route)(app, db);
});
//set prefix
if(config.prefix) {
console.log("Prefix: " + config.prefix);
app.use(config.prefix, app._router);
}
function getIndexParams() {
let params = [];
//get all routes with the exception of root-route
const routes = app._router.stack.filter((r) => r.route && r.route.path !== '/').sort((a,b) => a.route.path>b.route.path);
for(let r of routes) {
params.push({
path: r.route.path,
methods: Object.keys(r.route.methods).filter((m) => m !== '_all')
});
}
return {
prefix: config.prefix,
params: params
};
}
module.exports = app;