当前位置:网站首页>NodeJs - Express 中间件修改 Header: TypeError [ERR_INVALID_CHAR]: Invalid character in header content
NodeJs - Express 中间件修改 Header: TypeError [ERR_INVALID_CHAR]: Invalid character in header content
2022-07-02 06:21:00 【原小明】
背景
使用 Express 中间件进行数据代理方法,用户信息统一使用 Redis 缓存
- user-redis
const user_redis = async function (req, res, next) {
const token = req.headers['authorization']
const user = await redis.client.getAsync(token).then((data) => {
return data
})
// 给 header 添加用户信息,结果抛下面异常
req.headers.user_info = user
next()
}
异常信息
TypeError [ERR_INVALID_CHAR]: Invalid character in header content ["user_info"]
at ClientRequest.setHeader (_http_outgoing.js:473:3)
at new ClientRequest (_http_client.js:193:14)
......
解决方法
参考:
https://github.com/expressjs/express/issues/3401
将 user 信息进行 base64 编码后,在应用层进行解码即可;
- user_redis
const user_redis = async function (req, res, next) {
const token = req.headers['authorization']
const user = await redis.client.getAsync(token).then((data) => {
return data
})
// 给 header 添加用户信息,结果抛下面异常
req.headers.user_info = Buffer.from(user).toString('base64')
next()
}
- application
const user_str = req.headers.user_info
const userInfo = Buffer.from(user_str,'base64').toString()
边栏推荐
猜你喜欢
LeetCode 90. Subset II
栈(线性结构)
Browser principle mind map
代码技巧——Controller参数注解@RequestParam
Code skills - Controller Parameter annotation @requestparam
State machine in BGP
Sparse array (nonlinear structure)
华为MindSpore开源实习机试题
Contest3147 - game 38 of 2021 Freshmen's personal training match_ G: Flower bed
Lucene Basics
随机推荐
LeetCode 39. 组合总和
压力测试修改解决方案
State machine in BGP
Redis——热点key问题
ShardingSphere-JDBC篇
CUDA与Direct3D 一致性
kali最新更新指南
IPv6 experiment and summary
深入学习JVM底层(三):垃圾回收器与内存分配策略
TensorRT的数据格式定义详解
Hydration failed because the initial UI does not match what was rendered on the server. One of the reasons for the problem
标签属性disabled selected checked等布尔类型赋值不生效?
最新CUDA环境配置(Win10 + CUDA 11.6 + VS2019)
Use of Arduino wire Library
锐捷EBGP 配置案例
LeetCode 90. Subset II
The Chinese word segmentation task is realized by using traditional methods (n-gram, HMM, etc.), neural network methods (CNN, LSTM, etc.) and pre training methods (Bert, etc.)
RestTemplate请求时设置请求头,请求参数,请求体。
日志(常用的日志框架)
Invalid operation: Load into table ‘sources_orderdata‘ failed. Check ‘stl_load_errors‘ system table