Created
June 12, 2020 00:42
-
-
Save crojasaragonez/ce8f07f5340b891a51737eb7983bc978 to your computer and use it in GitHub Desktop.
Cors with custom headers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//server code | |
const express = require('express') | |
const cors = require('cors') | |
const app = express() | |
const port = 3000 | |
//whitelist of custom headers | |
app.use(cors({origin: '*', exposedHeaders: 'X-LH-Auth,X-Powered-By' })) | |
app.get('/', (req, res) => { | |
res.set({ | |
'Content-Type': 'text/plain', | |
'Content-Length': '123', | |
'X-LH-Auth': 'value' | |
}) | |
res.json({msg: 'CORS is enabled with custom headers'}); | |
}) | |
app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`)) | |
//client code | |
fetch('http://localhost:3000', {mode: 'cors', cache: 'no-cache'}).then(response => { | |
console.log(response.headers.get('Content-Type')); | |
console.log(response.headers.get('X-LH-Auth')); | |
console.log(response.headers.get('X-Powered-By')); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment