aboutsummaryrefslogtreecommitdiffhomepage
path: root/server.ts
diff options
context:
space:
mode:
authorRigel Kent <sendmemail@rigelk.eu>2021-06-01 13:25:41 +0200
committerChocobozzz <chocobozzz@cpy.re>2021-06-02 16:57:07 +0200
commit1cfbdd30d9913bfaa0c7e54f82e5b953646bb0d1 (patch)
tree1c3a8e3123da47e264533727f5e479edb5cda316 /server.ts
parent76148b27f7501bac061992136852be4303370c8d (diff)
downloadPeerTube-1cfbdd30d9913bfaa0c7e54f82e5b953646bb0d1.tar.gz
PeerTube-1cfbdd30d9913bfaa0c7e54f82e5b953646bb0d1.tar.zst
PeerTube-1cfbdd30d9913bfaa0c7e54f82e5b953646bb0d1.zip
refactor deprecated body-parser usage
Diffstat (limited to 'server.ts')
-rw-r--r--server.ts37
1 files changed, 20 insertions, 17 deletions
diff --git a/server.ts b/server.ts
index 1834256d5..66c9173ca 100644
--- a/server.ts
+++ b/server.ts
@@ -7,7 +7,6 @@ if (isTestInstance()) {
7} 7}
8 8
9// ----------- Node modules ----------- 9// ----------- Node modules -----------
10import * as bodyParser from 'body-parser'
11import * as express from 'express' 10import * as express from 'express'
12import * as morgan from 'morgan' 11import * as morgan from 'morgan'
13import * as cors from 'cors' 12import * 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
173app.use(apiResponseHelpers)
174
173// For body requests 175// For body requests
174app.use(bodyParser.urlencoded({ extended: false })) 176app.use(express.urlencoded({ extended: false }))
175app.use(bodyParser.json({ 177app.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
188app.use(advertiseDoNotTrack) 195app.use(advertiseDoNotTrack)
189 196
190// Response helpers used in developement
191app.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
226app.use(function (req, res, next) { 230app.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
232app.use(function (err, req, res, next) { 234// Catch thrown errors
235app.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,