From d1a06866c95b9eb9aebf412d391a3ab12797fca5 Mon Sep 17 00:00:00 2001 From: Denis Shatskiy Date: Mon, 11 Aug 2025 16:03:51 +0700 Subject: [PATCH] feat: Enhance CORS support by exposing important headers and update API responses to include Last-Modified and Content-Length-Source for improved client-side handling --- backend/server.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/backend/server.js b/backend/server.js index b1be61b..ead40e6 100644 --- a/backend/server.js +++ b/backend/server.js @@ -14,6 +14,12 @@ app.use(cors()); app.use(express.json()); app.use(compression()); +// Expose important headers to browser JS (for CORS) +app.use((req, res, next) => { + res.setHeader('Access-Control-Expose-Headers', 'ETag, Last-Modified, Content-Length-Source'); + next(); +}); + // Serve static files from the React app app.use(express.static(path.join(__dirname, 'public'))); @@ -377,6 +383,11 @@ app.post('/api/asns', async (req, res) => { try { const put = await s3.putObject(params).promise(); if (put.ETag) res.set('ETag', String(put.ETag)); + try { + const head = await s3.headObject({ Bucket: BUCKET_NAME, Key: 'bgp_data/asns.txt' }).promise(); + if (head.LastModified) res.set('Last-Modified', new Date(head.LastModified).toUTCString()); + if (typeof head.ContentLength === 'number') res.set('Content-Length-Source', String(head.ContentLength)); + } catch {} res.send('File updated successfully'); } catch (error) { console.error(error); @@ -460,6 +471,11 @@ app.post('/api/domains-new', async (req, res) => { try { const put = await s3.putObject(params).promise(); if (put.ETag) res.set('ETag', String(put.ETag)); + try { + const head = await s3.headObject({ Bucket: BUCKET_NAME, Key: 'bgp_data/domains_community.txt' }).promise(); + if (head.LastModified) res.set('Last-Modified', new Date(head.LastModified).toUTCString()); + if (typeof head.ContentLength === 'number') res.set('Content-Length-Source', String(head.ContentLength)); + } catch {} res.send('File updated successfully'); } catch (error) { console.error(error); @@ -543,6 +559,11 @@ app.post('/api/ip-ranges', async (req, res) => { try { const put = await s3.putObject(params).promise(); if (put.ETag) res.set('ETag', String(put.ETag)); + try { + const head = await s3.headObject({ Bucket: BUCKET_NAME, Key: 'bgp_data/ips.txt' }).promise(); + if (head.LastModified) res.set('Last-Modified', new Date(head.LastModified).toUTCString()); + if (typeof head.ContentLength === 'number') res.set('Content-Length-Source', String(head.ContentLength)); + } catch {} res.send('File updated successfully'); } catch (error) { console.error(error);