当前位置:网站首页>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()
边栏推荐
- Contest3147 - game 38 of 2021 Freshmen's personal training match_ A: chicken
- CUDA中内置的Vector类型和变量
- In depth understanding of JUC concurrency (I) what is JUC
- 自学table au
- Redis——Cluster数据分布算法&哈希槽
- Network related knowledge (Hardware Engineer)
- Introduce two automatic code generators to help improve work efficiency
- Classic literature reading -- deformable Detr
- 深入学习JVM底层(五):类加载机制
- TensorRT的数据格式定义详解
猜你喜欢
随机推荐
CUDA中的动态全局内存分配和操作
日志(常用的日志框架)
Redis——大Key問題
最新CUDA环境配置(Win10 + CUDA 11.6 + VS2019)
Log (common log framework)
注解和反射详解以及运用
CUDA中的Warp matrix functions
Redis——热点key问题
锐捷EBGP 配置案例
LeetCode 90. Subset II
Name six schemes to realize delayed messages at one go
I/o multiplexing & event driven yyds dry inventory
【程序员的自我修养]—找工作反思篇二
BGP 路由优选规则和通告原则
Decryption skills of encrypted compressed files
TensorRT的数据格式定义详解
Flask-Migrate 检测不到db.string() 等长度变化
自学table au
VRRP之监视上行链路
Contest3147 - game 38 of 2021 Freshmen's personal training match_ A: chicken








