-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
67 lines (60 loc) · 2.47 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
/**
* Binger App.
* Web application that provides a user-friendly interface for searching and watching movies and TV shows.
* @author Justin Hartman <code@justhart.com>
* @copyright Copyright (c) 2024, Justin Hartman <https://justhart.com>
* @link https://binger.uk Binger UK
* @license MIT
*/
/** @inheritDoc */
const express = require('express');
const app = express();
const path = require('path');
const appConfig = require('./config/app');
const connectDB = require('./config/db');
const appHelper = require('./helpers/appHelper');
/**
* Middleware function that sets the APP_URL as a local variable for the views.
* @param {Request} req - The request object containing the HTTP request details.
* @param {Response} res - The response object containing the HTTP response details.
* @param {NextFunction} next - The next middleware function in the chain.
*/
app.use((req, res, next) => {
res.locals.APP_NAME = appConfig.APP_NAME;
res.locals.APP_SUBTITLE = appConfig.APP_SUBTITLE;
res.locals.APP_DESCRIPTION = appConfig.APP_DESCRIPTION;
res.locals.APP_URL = appConfig.APP_URL;
// We have different card types based on whether the app uses MongoDB or not.
res.locals.CARD_TYPE = appHelper.useAuth ? 'card-add' : 'card';
next();
});
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
/**
* Uses the provided routes middleware for the specified path.
* @param {String} path - The path to the directory containing the static files.
* @returns {void} - No return value.
*/
app.use(express.static('public'));
/**
* Load standard routes and conditionally use additional routes based on the value of useAuth boolean.
* The method checks if MONGO_DB_URI is true then connects to MongoDB and uses additional middleware.
*/
app.use('/', require('./routes/app'));
// Test if MONGO_DB_URI is set.
if (appHelper.useAuth) {
// Connect to MongoDB instance.
connectDB().catch(e => console.log(e.message));
// Use additional routes.
app.use('/user', require('./routes/auth'));
app.use('/watchlist', require('./routes/watchlist'));
}
/**
* Starts the server and listens on the specified port.
* @param {String} API_HOST - The host name on which the server will listen on.
* @param {Number} API_PORT - The port number on which the server will listen on.
* @returns {void} - No return value.
*/
app.listen(appConfig.API_PORT, appConfig.API_HOST, () => {
console.log(`Server is running on http://${appConfig.API_HOST}:${appConfig.API_PORT}`);
});