1. 暴露自定义响应头
app.post('/api/user/hello', (req, res) => {
res.setHeader('Access-Control-Allow-Origin', '*')
res.setHeader('Access-Control-expose-Headers', 'myHeader')
res.set('myHeader', 123)
res.json({ hello: 'world' })
})
fetch('http://localhost:3000/api/user/hello', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
}).then(res => {
console.log(res.headers.get('myHeader'))
return res.json()
}).then(response => {
})
预检请求(options)的时机
- POST 请求并且
'Content-Type'
为 'application/json'
。 - 跨域
- 自定义响应头