UNPKG

4.73 kBJavaScriptView Raw
1// __ __ ____ _____ _ _ _ ______ _____
2// | \/ | / __ \ | __ \ | | | | | | | ____| / ____|
3// | \ / | | | | | | | | | | | | | | | | |__ | (___
4// | |\/| | | | | | | | | | | | | | | | | __| \___ \
5// | | | | | |__| | | |__| | | |__| | | |____ | |____ ____) |
6// |_| |_| \____/ |_____/ \____/ |______| |______| |_____/
7//
8
9//
10// Module for working with file and directory paths
11//
12let path = require('path');
13
14//
15// HTTP request logger middleware for NodeJS
16//
17let logger = require('morgan');
18
19//
20// Fast, unopinionated, minimalist web framework
21//
22let express = require('express');
23
24//
25// Parse incoming request bodies in a middleware before your handlers,
26// available under the req.body property.
27//
28let body_parser = require('body-parser');
29
30//
31// Save the express framework in a simple variable
32//
33let app = express();
34
35// _____ ______ _______ _______ _____ _ _ _____ _____
36// / ____| | ____| |__ __| |__ __| |_ _| | \ | | / ____| / ____|
37// | (___ | |__ | | | | | | | \| | | | __ | (___
38// \___ \ | __| | | | | | | | . ` | | | |_ | \___ \
39// ____) | | |____ | | | | _| |_ | |\ | | |__| | ____) |
40// |_____/ |______| |_| |_| |_____| |_| \_| \_____| |_____/
41//
42
43//
44// Remove the information about what type of framework is the site running on
45//
46app.disable('x-powered-by');
47
48//
49// Remove from the header the ETag since we have no use for it in this case,
50// and this will also reduce the response size by 19%
51//
52app.set('etag', false);
53
54//
55// Remove the date header if you know the other side won't cash the data.
56// Since this is the majority of the time, we remove this by default.
57//
58app.use(function(req, res, next) {
59
60 //
61 // 1. Remove the Date header from each response
62 //
63 res.removeHeader('Date');
64
65 //
66 // -> Keep going
67 //
68 next();
69
70});
71
72//
73// When enabled, Express attempts to determine the IP address of the client
74// connected through the front-facing proxy, or series of proxies.
75//
76app.enable('trust proxy');
77
78//
79// HTTP request logger middleware for node.js
80//
81app.use(logger('dev'));
82
83//
84// Parse all request as regular text, and not JSON objects
85//
86app.use(body_parser.json());
87
88//
89// Parse application/x-www-form-urlencoded
90//
91app.use(body_parser.urlencoded({ extended: false }));
92
93// _____ ____ _ _ _______ ______ _____
94// | __ \ / __ \ | | | | |__ __| | ____| / ____|
95// | |__) | | | | | | | | | | | | |__ | (___
96// | _ / | | | | | | | | | | | __| \___ \
97// | | \ \ | |__| | | |__| | | | | |____ ____) |
98// |_| \_\ \____/ \____/ |_| |______| |_____/
99//
100
101////////////////////////////////////////////////////////////////////////////////
102
103app.use(require('./routes/https'));
104
105////////////////////////////////////////////////////////////////////////////////
106
107// ______ _____ _____ ____ _____ _____
108// | ____| | __ \ | __ \ / __ \ | __ \ / ____|
109// | |__ | |__) | | |__) | | | | | | |__) | | (___
110// | __| | _ / | _ / | | | | | _ / \___ \
111// | |____ | | \ \ | | \ \ | |__| | | | \ \ ____) |
112// |______| |_| \_\ |_| \_\ \____/ |_| \_\ |_____/
113//
114
115//
116//
117// If nonce of the above routes matches, we create an error to let the
118// user know that the URL accessed doesn't match anything.
119//
120app.use(function(req, res, next) {
121
122 //
123 // 1. Create a visual message for a human
124 //
125 let error = new Error("Not Found");
126
127 //
128 // 2. The request is: Not Found.
129 //
130 error.status = 404;
131
132 //
133 // -> Move to the next middelware
134 //
135 next(error);
136
137});
138
139//
140// Display any error that occurred during the request.
141//
142app.use(function(error, req, res, next) {
143
144 //
145 // 1. Use the status of the error itself or set a default one.
146 //
147 let status = error.status || 500;
148
149 //
150 // 2. If there was no status, and the default was set, we have to
151 // add the status to the error object.
152 //
153 if(status === 500)
154 {
155 error.status = 500;
156 }
157
158 //
159 // 3. Set the basic information about the error, that is going to be
160 // displayed to user and developers - regardless.
161 //
162 let obj_message = {
163 message: error.message
164 }
165
166 //
167 // 4. Don't log the error when in production
168 //
169 if(process.env.NODE_ENV != 'production')
170 {
171 //
172 // 1. Set the variable to show the stack-trace to the developer
173 //
174 obj_message.error = error;
175
176 //
177 // -> Show the error in the console
178 //
179 console.error(error);
180 }
181
182 //
183 // 6. Set the status response as the one from the error message
184 //
185 res.status(status);
186
187 //
188 // 7. Send the error
189 //
190 res.json(obj_message);
191
192 //
193 // -> Stop the request
194 //
195 res.end();
196
197});
198
199module.exports = app;