node.js - 如何在下一个 js 中访问方法内的请求标头?
问题描述
我是使用下一个 js 的新手,有什么方法可以访问方法内的请求标头,而无需将 de 值作为参数传递给方法?我需要在方法中记录一个名为“x-transaction-id”的标头,但我不想重新传递参数。
例如,这是我的控制器
export default nextConnect()
.use(AuthHandler)
.use(RequestContextMiddleware) // NEED TO BE BEFORE LOGGER MIDDLEWARE
.use(LoggerMiddleware)
.get(async (req: ExtendedApiRequest, res: NextApiResponse<ResumeCountResponse>) => {
try {
const { cookies, userId } = req;
const transactionId = req.headers['x-transaction-id']
const resumeService = ResumeServiceFactory.make();
const resumeCount = await resumeService.getResumeListCountOfUser(cookies.cactk, userId);
res.status(200).json({ data: resumeCount, error: null });
} catch (error) {
res.status(500).json({ data: null, error: error.message });
}
});
我需要直接在服务方法中记录事务 ID
class ResumeService {
cache: Cache;
constructor(cache: Cache) {
this.cache = cache;
}
async getResumeListCountOfUser(token: string, userId: number): Promise<number> {
try {
try {
const dataCache = await this.cache.readCache("any_key");
if (dataCache) {
const result = JSON.parse(dataCache);
return result.data.length;
}
} catch (error) {
const transactionId = ?????
Logger.error(
`[Read cache error resumeService]: ${error.message}`,
transactionId,
error,
);
}
}
}
解决方案
推荐阅读
- python - 通过 python 中的 xlwings 调整 Excel 中的数字格式
- c++ - 创建 QWebSocket 对象时,它会显示 QSslSocket 警告消息
- c# - 如何打印或显示两个字符串列表的所有组合
- mongodb - 为什么我的 mongo 数据库被攻击者删除了?
- go - 从 setReadDeadline 定义的 net.Listen (tcp) 检查超时错误
- docker - Docker /bin/sh 需要 10 秒才能停止
- python - 我在 pygame 中的事件循环有问题
- python - 有没有更优雅的方法来重构这个非常线性的代码块?
- python - 下面算法 O(logn) 的空间复杂度如何?
- pyspark - Pyspark join 不接受 5 个位置参数?