diff options
Diffstat (limited to 'server.ts')
-rw-r--r-- | server.ts | 37 |
1 files changed, 20 insertions, 17 deletions
@@ -7,7 +7,6 @@ if (isTestInstance()) { | |||
7 | } | 7 | } |
8 | 8 | ||
9 | // ----------- Node modules ----------- | 9 | // ----------- Node modules ----------- |
10 | import * as bodyParser from 'body-parser' | ||
11 | import * as express from 'express' | 10 | import * as express from 'express' |
12 | import * as morgan from 'morgan' | 11 | import * as morgan from 'morgan' |
13 | import * as cors from 'cors' | 12 | import * as cors from 'cors' |
@@ -170,14 +169,22 @@ app.use(morgan('combined', { | |||
170 | skip: req => CONFIG.LOG.LOG_PING_REQUESTS === false && req.originalUrl === '/api/v1/ping' | 169 | skip: req => CONFIG.LOG.LOG_PING_REQUESTS === false && req.originalUrl === '/api/v1/ping' |
171 | })) | 170 | })) |
172 | 171 | ||
172 | // Response helpers used for errors | ||
173 | app.use(apiResponseHelpers) | ||
174 | |||
173 | // For body requests | 175 | // For body requests |
174 | app.use(bodyParser.urlencoded({ extended: false })) | 176 | app.use(express.urlencoded({ extended: false })) |
175 | app.use(bodyParser.json({ | 177 | app.use(express.json({ |
176 | type: [ 'application/json', 'application/*+json' ], | 178 | type: [ 'application/json', 'application/*+json' ], |
177 | limit: '500kb', | 179 | limit: '500kb', |
178 | verify: (req: express.Request, _, buf: Buffer) => { | 180 | verify: (req: express.Request, res: express.Response, buf: Buffer) => { |
179 | const valid = isHTTPSignatureDigestValid(buf, req) | 181 | const valid = isHTTPSignatureDigestValid(buf, req) |
180 | if (valid !== true) throw new Error('Invalid digest') | 182 | if (valid !== true) { |
183 | res.fail({ | ||
184 | status: HttpStatusCode.FORBIDDEN_403, | ||
185 | message: 'Invalid digest' | ||
186 | }) | ||
187 | } | ||
181 | } | 188 | } |
182 | })) | 189 | })) |
183 | 190 | ||
@@ -187,9 +194,6 @@ app.use(cookieParser()) | |||
187 | // W3C DNT Tracking Status | 194 | // W3C DNT Tracking Status |
188 | app.use(advertiseDoNotTrack) | 195 | app.use(advertiseDoNotTrack) |
189 | 196 | ||
190 | // Response helpers used in developement | ||
191 | app.use(apiResponseHelpers) | ||
192 | |||
193 | // ----------- Views, routes and static files ----------- | 197 | // ----------- Views, routes and static files ----------- |
194 | 198 | ||
195 | // API | 199 | // API |
@@ -222,23 +226,22 @@ if (cliOptions.client) app.use('/', clientsRouter) | |||
222 | 226 | ||
223 | // ----------- Errors ----------- | 227 | // ----------- Errors ----------- |
224 | 228 | ||
225 | // Catch 404 and forward to error handler | 229 | // Catch unmatched routes |
226 | app.use(function (req, res, next) { | 230 | app.use((req, res: express.Response) => { |
227 | const err = new Error('Not Found') | 231 | res.status(HttpStatusCode.NOT_FOUND_404).end() |
228 | err['status'] = HttpStatusCode.NOT_FOUND_404 | ||
229 | next(err) | ||
230 | }) | 232 | }) |
231 | 233 | ||
232 | app.use(function (err, req, res, next) { | 234 | // Catch thrown errors |
235 | app.use((err, req, res: express.Response, next) => { | ||
236 | // Format error to be logged | ||
233 | let error = 'Unknown error.' | 237 | let error = 'Unknown error.' |
234 | if (err) { | 238 | if (err) { |
235 | error = err.stack || err.message || err | 239 | error = err.stack || err.message || err |
236 | } | 240 | } |
237 | 241 | // Handling Sequelize error traces | |
238 | // Sequelize error | ||
239 | const sql = err.parent ? err.parent.sql : undefined | 242 | const sql = err.parent ? err.parent.sql : undefined |
240 | |||
241 | logger.error('Error in controller.', { err: error, sql }) | 243 | logger.error('Error in controller.', { err: error, sql }) |
244 | |||
242 | return res.fail({ | 245 | return res.fail({ |
243 | status: err.status || HttpStatusCode.INTERNAL_SERVER_ERROR_500, | 246 | status: err.status || HttpStatusCode.INTERNAL_SERVER_ERROR_500, |
244 | message: err.message, | 247 | message: err.message, |