【问题标题】:Not able to access request Headers in node.js server无法访问 node.js 服务器中的请求标头
【发布时间】:2022-07-06 18:54:48
【问题描述】:

这与我的上一个问题有关 - JsonWebTokenError: jwt must be a string, node.js

我正在尝试变得简单
console.log(req.header('host'),'req host')
但是像我一样获取整个标题对象会进入
console.log(req),'req')

为什么我无法访问 header 对象的值,我也做了 Cors 设置以避免自定义 header 访问,仍然没有成功?

Post.js(通过 post 请求发送标头)

import React, { useEffect, useState } from 'react'
import { useParams } from 'react-router-dom';
import axios from 'axios';
import './Post.css'


function Post() {

    let { id } = useParams();

    const [postObject, setPostObject] = useState({})
    const [comments, setComments] = useState([]);
    const [newComment, setNewComment] = useState("");

    // console.log(comments)




    const addComment = () => {


        const accessToken = sessionStorage.getItem('accessToken')
        console.log(typeof (accessToken), 'acces token in comment button')


        axios.post(`http://localhost:4000/comments`, {
            commentBody: newComment,
            PostId: id
        },
            {
                headers: {
                    accessToken: accessToken,
                }
            }

        )
            .then((res) => {
               
                const data = res.data;
                console.log(data, 'comments')

                setComments([...comments, data])
                setNewComment("")
            })
            .catch((err) => {
                alert(err, 'Error:comment')
            })
    }

    return (
        <div className='Post'>

            <div className='left__side'>
                <div className='left__side__wrapper'>

                    <div className='title'>{postObject.title}</div>
                    <div className='text'>{postObject.postText}</div>
                    <div className='username'>{postObject.username}</div>
                </div>


            </div>
            <div className='right__side'>
                <div className='right__side__wrapper'>
                    <div className='add__comment__container'>

                        <input type="text"
                            value={newComment}
                            placeholder="Comment"
                            //  autoComplete="off"
                            onChange={(e) => setNewComment(e.target.value)}

                        />
                        <button onClick={addComment}> Submit Comment</button>

                    </div>
                    <div className='listOfCommnets'>

                        {comments.map((item, index) => {
                            {/* console.log(item, 'item') */ }
                            return <div className='comments' key={index}>Comments:<br />{item.commentBody}</div>

                        })}
                    </div>
                </div>
            </div>

        </div>
    )
}

export default Post

AuthMiddleware.js(从前端获取标头或请求标头)

const { verify } = require("jsonwebtoken")


const validateToken = (res, req, next) => {

    console.log(req, 'req')

    console.log(req.header('host'), 'req host')
}

module.exports = { validateToken }

【问题讨论】:

    标签: javascript node.js server http-headers backend


    【解决方案1】:

    如果这是 Express 我认为没有 req.header('host') 方法,请尝试 req.get('host')

    【讨论】:

      猜你喜欢
      • 2012-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-26
      • 2017-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多