最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get request headers in express? - Stack Overflow

programmeradmin3浏览0评论

I writing code for authorization. After click on the button for login I create a new header with

res.header('Authorization', token)

admin_login router:

router.post('/admin_login', async (req, res) => {
    const adminDB = data.admins;
    const admin = adminDB.find(admin => req.body.email === admin.email)
    if (!admin) return res.status(400).send('Email in not found!')
    if (admin.password !== req.body.password) return res.status(400).send('Invalid password')
    const token = jwt.sign({ admin }, 'the_secret_key')
    res.header('Authorization', token)
    res.redirect('/admin')
})

I wont to recieve Authorization header after login in admin router, but I wont recieve it. I see Authorization header in

Code for verification:

const jwt = require('jsonwebtoken')

module.exports = (req, res, next) => {
    const token = req.header('Authorization')
    console.log(token)

    if (!token) return res.status(401).send('Access Denied')

    try {
        const verified = jwt.verify(token, 'the_secret_key')
        req.admin = verified
        next()
    } catch (e) {
        res.status(400).send('Invalid token')
    }
}

first img: headers in admin router
second img: headers in admin_login router after click on the button for login



Please, help me

I writing code for authorization. After click on the button for login I create a new header with

res.header('Authorization', token)

admin_login router:

router.post('/admin_login', async (req, res) => {
    const adminDB = data.admins;
    const admin = adminDB.find(admin => req.body.email === admin.email)
    if (!admin) return res.status(400).send('Email in not found!')
    if (admin.password !== req.body.password) return res.status(400).send('Invalid password')
    const token = jwt.sign({ admin }, 'the_secret_key')
    res.header('Authorization', token)
    res.redirect('/admin')
})

I wont to recieve Authorization header after login in admin router, but I wont recieve it. I see Authorization header in

Code for verification:

const jwt = require('jsonwebtoken')

module.exports = (req, res, next) => {
    const token = req.header('Authorization')
    console.log(token)

    if (!token) return res.status(401).send('Access Denied')

    try {
        const verified = jwt.verify(token, 'the_secret_key')
        req.admin = verified
        next()
    } catch (e) {
        res.status(400).send('Invalid token')
    }
}

first img: headers in admin router
second img: headers in admin_login router after click on the button for login



Please, help me

Share Improve this question asked Mar 1, 2020 at 15:04 Alice ArgentAlice Argent 391 gold badge1 silver badge6 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 12

As far as I can tell from the documentation, there is no header method on the Express Request object or the Node.js IningMessage it extends.

The Express documentation says that to retrieve a header, you use get:

req.get(field)

Returns the specified HTTP request header field (case-insensitive match).

So:

const token = req.get("Authorization");

Or you could use the headers object from IningMessage:

const token = req.headers["authorization"];

(Note the lower case, Node.js makes the header names lower case when building the object.)

You should use the req.get(headerName) method

发布评论

评论列表(0)

  1. 暂无评论