当前位置:网站首页>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()
边栏推荐
猜你喜欢

Pbootcms collection and warehousing tutorial quick collection release

Invalid operation: Load into table ‘sources_orderdata‘ failed. Check ‘stl_load_errors‘ system table

Sparse array (nonlinear structure)

State machine in BGP

CUDA中的Warp Shuffle

栈(线性结构)

链表(线性结构)

一口气说出 6 种实现延时消息的方案

深入学习JVM底层(四):类文件结构

Redis - big key problem
随机推荐
Bgp Routing preference Rules and notice Principles
WLAN相关知识点总结
RestTemplate请求时设置请求头,请求参数,请求体。
Amazon AWS data Lake Work Pit 1
LeetCode 47. 全排列 II
Linked list (linear structure)
Contest3147 - game 38 of 2021 Freshmen's personal training match_ 1: Maximum palindromes
LeetCode 39. Combined sum
CUDA中的Warp matrix functions
Learn about various joins in SQL and their differences
Invalid operation: Load into table ‘sources_ orderdata‘ failed. Check ‘stl_ load_ errors‘ system table
日期时间API详解
Sentinel 阿里开源流量防护组件
MySql索引
【程序员的自我修养]—找工作反思篇二
Hydration failed because the initial UI does not match what was rendered on the server. One of the reasons for the problem
LeetCode 39. 组合总和
PgSQL学习笔记
日志 - 7 - 记录一次丢失文件(A4纸)的重大失误
Is there a really free applet?