Archnemix commited on
Commit
b39e375
·
verified ·
1 Parent(s): c4d9369

Update src/index.js

Browse files
Files changed (1) hide show
  1. src/index.js +17 -14
src/index.js CHANGED
@@ -1,5 +1,6 @@
1
  import express from 'express'
2
  import router from './router.js'
 
3
 
4
  const REQUIRED_ENV = [
5
  'DISCORD_PUBLIC_KEY',
@@ -17,21 +18,23 @@ for (const key of REQUIRED_ENV) {
17
 
18
  const app = express()
19
 
20
- // Custom middleware — must come before the router.
21
- // Every request gets req.rawBody as the original string.
22
- // Non-/interactions routes also get req.body as parsed JSON.
23
  app.use((req, res, next) => {
24
- let data = ''
25
- req.setEncoding('utf8')
26
- req.on('data', (chunk) => { data += chunk })
27
- req.on('end', () => {
28
- req.rawBody = data
29
- if (req.path !== '/interactions') {
30
- try { req.body = JSON.parse(data) } catch { req.body = {} }
31
- }
32
- next()
33
- })
34
- })
 
 
 
 
 
35
 
36
  app.use('/', router)
37
 
 
1
  import express from 'express'
2
  import router from './router.js'
3
+ import bodyParser from 'body-parser';
4
 
5
  const REQUIRED_ENV = [
6
  'DISCORD_PUBLIC_KEY',
 
18
 
19
  const app = express()
20
 
 
 
 
21
  app.use((req, res, next) => {
22
+ // Only /interactions needs raw body for signature verification
23
+ if (req.path === '/interactions') {
24
+ bodyParser.raw({
25
+ type: 'application/json',
26
+ limit: '10mb'
27
+ })(req, res, (err) => {
28
+ if (err) return next(err);
29
+
30
+ req.rawBody = req.body; // This is now a Buffer (best)
31
+ next();
32
+ });
33
+ } else {
34
+ // Other routes can use JSON
35
+ bodyParser.json({ limit: '10mb' })(req, res, next);
36
+ }
37
+ });
38
 
39
  app.use('/', router)
40