[英]Axios - Different headers for Express Request
我开始开发SSR React-Redux应用程序,而axios标头有问题。
为每个Express请求设置不同的标头的正确方法是什么?
Express.get('*', async (req, res) => { axios.defaults.headers.common = req.headers['my-customer-id']; // first axios get request // second axios get request // thirth axios get request //... initialization React App });
通过这种方法,axios默认头将从上一个Express请求中覆盖。
我尝试使用axios.create()
创建不同的axios实例,但是我不知道在何处以及如何保存该实例,以便在应用程序模块内部使用。
您可以使用create创建 axios的新自定义实例。
const customAxios = axios.create({
headers: {...req.headers['my-customer-id']}
});
现在使用customAxios发出请求
customAxios.get(...)
全局头和仅实例头之间有区别:
// affects the global instance
axios.defaults.headers.common['Auth-Token'] = 'foo bar';
// affects to a single instance
axiosInstance.defaults.headers['Auth-Token'] = 'foo bar';
这也适用于expressjs应用程序中的我,具有不同的服务(axios实例)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.