-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
126 lines (113 loc) · 4.41 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import express from 'express';
import path from 'path';
import router from './routes';
import cookieSession from 'cookie-session';
import { logToFile } from './service/bookLibraryService';
import dotenv from 'dotenv';
// Load environment variables from the .env file
dotenv.config();
const app = express();
const PORT = process.env.PORT || 3000;
const logPathDir = path.join(__dirname, 'logs');
app.set('trust proxy', 1);
app.use(
cookieSession({
name: 'session',
keys: ['askdjas2617', 'jaskdlkasd879'],
})
);
app.use(express.json());
app.use((request, response, next) => {
logToFile(logPathDir, `Request received: ${request.method} ${request.url}`);
next();
});
/**
* Configures the view engine for rendering templates in the application.
*
* @param {string} setting - The name of the setting to configure. In this case, it is 'view engine'.
* @param {string} engine - The name of the template engine to use. Here, it is 'ejs' (Embedded JavaScript Templates).
*/
app.set('view engine', 'ejs');
/**
* Configures the directory for the application's view templates.
*
* @param {string} setting - The name of the setting to configure. In this case, it is 'views'.
* @param {string} path - The absolute path to the directory containing the view templates.
* This is set to './views' relative to the current directory.
*/
app.set('views', path.join(__dirname, './views'));
/**
* Serves static files from the specified directory.
*
* @param {string} middleware - Middleware function to serve static files.
* @param {string} path - The absolute path to the directory containing static files. In this case, it points to the './public' folder.
*/
app.use(express.static(path.join(__dirname, './public')));
/**
* Mounts the specified router middleware to the root path of the application.
*
* @param {string} path - The base URL path where the router is mounted. In this case, '/' represents the root path.
* @param {function} router - A middleware function or a router instance that handles the routes and logic for the application.
*/
app.use('/', router());
/**
* Error Handling Middleware
*
* This middleware is responsible for handling 404 errors that occur within the application.
*
* @param {Object} request - The HTTP request object.
* @param {Object} response - The HTTP response object.
* @param {Function} next - The next middleware function in the Express request-response cycle.
*
* Functionality:
* - Logs 404 error in case of resource not found
*
* @returns {void}
*/
app.use((request, response, next) => {
const error = new Error('Resource not found');
error.statusCode = 404;
response.render('layout', { pageTitle: '404 Error ', template: '404' });
logToFile(logPathDir, `${error.stack}`);
next(error);
});
/**
* Error Handling Middleware
*
* This middleware is responsible for handling errors that occur within the application.
* It ensures that unhandled errors are logged and an appropriate response is sent to the client.
*
* @param {Error} error - The error object, typically passed from other routes or middleware.
* @param {Object} request - The HTTP request object.
* @param {Object} response - The HTTP response object.
* @param {Function} next - The next middleware function in the Express request-response cycle.
*
* Functionality:
* - Logs the error stack trace to the server console for debugging purposes.
* - Constructs a response with:
* - A status code derived from `error.status` (if available) or defaults to 500 (Internal Server Error).
* - An error message derived from `error.message` or defaults to "Something is broken!".
*
* Usage:
* - This middleware should be defined after all route handlers in the application.
* - Errors can be passed to this middleware using the `next(error)` function from any route or middleware.
*
* @returns {void}
*/
app.use((error, request, response, next) => {
const errorMessage = error.message || 'Something is broken!';
logToFile(logPathDir, `${error.stack}`);
response.status(error.status || 500).json({
success: false,
error: errorMessage,
});
});
/**
* Starts the Express server and listens for incoming connections on the specified port.
*
* @param {number} PORT - The port number on which the server will listen.
* @param {function} callback - A callback function that executes once the server starts successfully.
*/
app.listen(PORT, () => {
console.log(`Express sever is running on ${PORT}...`);
});