This repository has been archived by the owner on Dec 8, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
55 lines (47 loc) · 2.58 KB
/
index.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
require('dotenv').config(); // Config
require('./src/models/firebase/init/setupFirebase').initFirestore();// Load Firestore
const express = require('express'); // Our HTTP(s) service
const bodyParser = require('body-parser');
const nunjucks = require('express-nunjucks'); // Connect express and nunjucks to each other
const routes = require('./src/routers/routes'); // Our route file
const {Glob} = require("glob"); // Dynamically register routes
const path = require("path"); // Get the path of our root folder
// Set boot mode
const IS_DEV = process.env.NODE_ENV === 'development';
console.log('Development mode?:', IS_DEV);
console.log("Starting in " + process.env.NODE_ENV + " mode...")
// Initialize express
const APP = express();
const IP = process.env.EXPRESS_IP;
const PORT = process.env.EXPRESS_PORT || 8080;
// Configure express
APP.use(bodyParser.json());
APP.set('views', __dirname + '/src/views'); // Set views folder to 'views' so we can load our webpages
APP.set('view engine', 'njk'); // Set view engine (e.g. 'nunjucks')
APP.use(express.static(path.join(__dirname + '/src/public'))); // Set the folder to wherever we will store our static content
// Configure nunjucks
nunjucks(APP, {
watch: IS_DEV, // If true, the system will automatically update templates when they are changed on the filesystem.
noCache: IS_DEV, // if true, the system will avoid using a cache and templates will be recompiled every single time.
globals: { // Define global variables, so you can use them in your views
}
});
// Turn on caching
const setCache = function (req, res, next) {
const period = 60*5 // Cache life is 5 minutes
if (req.method === 'GET') res.set('Cache-control', `public, max-age=${period}`);
else res.set('Cache-control', `no-store`);
next();
}
APP.use(setCache);
// Register all controllers dynamically
const CONTROLLERS = new Glob('**/controllers/**/**.js', { withFileTypes: true })
CONTROLLERS.stream().on('data', path => {
let CONTROLLER = require(path.fullpath());
APP.use('/', CONTROLLER);
console.log('Found controller:', CONTROLLER);
})
// Handle page requests
APP.use('/', routes);
// Start server
APP.listen(PORT, () => console.log(`Server listening on http://${IP}:${PORT}!`));