Skip to content

Instantly share code, notes, and snippets.

@crojasaragonez
Created June 12, 2020 00:42
Show Gist options
  • Save crojasaragonez/ce8f07f5340b891a51737eb7983bc978 to your computer and use it in GitHub Desktop.
Save crojasaragonez/ce8f07f5340b891a51737eb7983bc978 to your computer and use it in GitHub Desktop.
Cors with custom headers
//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