Pepguy commited on
Commit
2fed52b
·
verified ·
1 Parent(s): c0f246c

Update app.js

Browse files
Files changed (1) hide show
  1. app.js +41 -2
app.js CHANGED
@@ -9,6 +9,45 @@ const axios = require('axios');
9
 
10
  const app = express();
11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  const {
13
  FIREBASE_CREDENTIALS,
14
  PAYSTACK_DEMO_SECRET,
@@ -744,8 +783,8 @@ app.post('/create-payment-link', express.json(), async (req, res) => {
744
  app.get('/health', (_req, res) => res.status(200).json({ ok: true }));
745
  app.post('/health', (_req, res) => res.status(200).json({ ok: true }));
746
 
747
- app.get('/', (_req, res) => res.status(200).json({ ok: true }));
748
- app.post('/', (_req, res) => res.status(200).json({ ok: true }));
749
 
750
 
751
  app.listen(PORT, () => {
 
9
 
10
  const app = express();
11
 
12
+ // ----
13
+
14
+ // Trust proxy headers (useful when behind load balancers / host proxies)
15
+ app.set('trust proxy', true);
16
+
17
+ // Normalize incoming path: remove trailing slash in-process (no redirect),
18
+ // so callers can hit /webhook/paystack or /webhook/paystack/ and we won't send redirects.
19
+ // This mutates req.url and keeps query string intact.
20
+ app.use((req, res, next) => {
21
+ try {
22
+ if (req.path && req.path.length > 1 && req.path.endsWith('/')) {
23
+ // remove only trailing slashes from pathname while keeping query string
24
+ const url = req.originalUrl || req.url;
25
+ // split once at '?'
26
+ const [pathPart, q] = url.split('?');
27
+ const normalized = pathPart.replace(/\/+$/, '') + (q ? `?${q}` : '');
28
+ req.url = normalized;
29
+ // Also update req.originalUrl so logs match
30
+ req.originalUrl = normalized;
31
+ }
32
+ } catch (e) {
33
+ // don't break the request flow for any reason
34
+ }
35
+ return next();
36
+ });
37
+
38
+ // Ensure OPTIONS and HEAD don't cause proxies to try redirects or 301 behaviour
39
+ app.options('*', (_req, res) => res.sendStatus(200));
40
+ app.head('*', (_req, res) => res.sendStatus(200));
41
+
42
+ // Optional light logger — helpful to confirm whether requests reach your app.
43
+ // Comment out if you want less noise.
44
+ app.use((req, res, next) => {
45
+ console.log(new Date().toISOString(), req.method, req.originalUrl, '-> ip:', req.ip || req.headers['x-forwarded-for'] || '');
46
+ next();
47
+ });
48
+
49
+ // ----
50
+
51
  const {
52
  FIREBASE_CREDENTIALS,
53
  PAYSTACK_DEMO_SECRET,
 
783
  app.get('/health', (_req, res) => res.status(200).json({ ok: true }));
784
  app.post('/health', (_req, res) => res.status(200).json({ ok: true }));
785
 
786
+ app.get('/', (_req, res) => res.status(204).end());
787
+ app.post('/', (_req, res) => res.status(204).end());
788
 
789
 
790
  app.listen(PORT, () => {