简体   繁体   English

NodeJS/Express - 无法读取所有 Req.Headers

[英]NodeJS/Express - Cannot Read All Req.Headers

Building a simple ToDo app with ReactJs frontend and NodeJs/Express backend.使用 ReactJs 前端和 NodeJs/Express 后端构建一个简单的 ToDo 应用程序。 I configured my frontend to include userId as a request header:我将我的前端配置为包含 userId 作为请求 header:

export default function authHeader() {
    const user = JSON.parse(localStorage.getItem('user'));
  
    if (user && user.accessToken) {
      // return { Authorization: 'Bearer ' + user.accessToken }; // for Spring Boot back-end
      return {
          'x-access-token': user.accessToken,
          'userid': user.id
      };       // for Node.js Express back-end
    } else {
      return {};
    }
  }

This header is included with the Axios request:这个 header 包含在 Axios 请求中:

// List all Group Members
listMembers() {
    return http.get(`/group`, { headers: authHeader() });
}

Consequently, I can see the headers in the request:因此,我可以在请求中看到标头:

在此处输入图像描述

Part of my auth middleware references the access token:我的身份验证中间件的一部分引用了访问令牌:

verifyToken = (req, res, next) => {
  let token = req.headers["x-access-token"];

  if (!token) {
    return res.status(403).send({
      message: "No token provided!"
    });
  }

  jwt.verify(token, config.secret, (err, decoded) => {
    if (err) {
      return res.status(401).send({
        message: "Unauthorized!"
      });
    }
    req.userId = decoded.id;
    next();
  });
};

The req.headers lines near the top is able to pull the token from header "x-access-token" with no problem.顶部附近的 req.headers 行能够毫无问题地从 header“x-access-token”中提取令牌。 HOWEVER - for debug purposes, I have tried to pull the header userId value instead using let token = req.headers["userid"];但是 - 出于调试目的,我尝试拉取 header userId 值,而不是使用let token = req.headers["userid"]; but this simply comes back as undefined in my debug tools.但这只是在我的调试工具中返回为未定义。 Why can it pull x-access token header but not userId?为什么它可以拉取 x-access token header 而不是 userId? My eventual goal is to refer to the req.header userId value in backend SQL queries, this is just a test.我最终的目标是在后端SQL查询中引用req.header userId值,这只是一个测试。

It ended being a capitalization error somewhere in pipeline.它最终成为管道中某处的大写错误。 @Sardar's comment was on the money, make sure everything is spelled correctly and matches case. @Sardar 的评论是关于钱的,确保所有内容都拼写正确并匹配大小写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM