Why I Can't get Auth header in Express?
我在
1 2 3 4 5 6 7 8 9 10 11 12 13 | const headers = { headers: { 'Authorization': `Bearer ${localStorage.token}`, } } API.post('validate', headers ) .then(res => { }) .catch(error => { }) |
在浏览器console.log中,我看到
1 2 3 4 5 6 | router.post('/', errorHandler(async (req, res, next) => { console.log(req.method) console.log(req.headers) let bearerHeader = req.headers['Authorization'] console.log(bearerHeader) }) |
这是Express中的console.log:
1 2 3 4 5 6 7 8 9 10 11 12 | POST { host: 'localhost:5000', connection: 'keep-alive', 'content-length': '244', accept: 'application/json, text/plain, */*', origin: 'http://localhost:3000', 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36', 'content-type': 'application/json;charset=UTF-8', referer: 'http://localhost:3000/signin', 'accept-encoding': 'gzip, deflate, br', 'accept-language': 'ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7,uk;q=0.6' } undefined |
我也使用cors库处理CORS(前端和后端的本地主机不同)
1 | import cors from 'cors' |
我在做什么错?如何在axios的帮助下将auth实际添加到标头?
POST \\的第二个参数是有效负载。第三个参数是标题。尝试这种方式: