gpt4 book ai didi

node.js - 无法在 express 中设置 HTTP header

转载 作者:可可西里 更新时间:2023-11-01 17:04:42 25 4
gpt4 key购买 nike

我一直在努力为我的应用程序实现 header ,所以我决定 copy some stack overflow code确保我没有犯任何错字:

addToHeader = function (req, res, next) {
console.log("add to header called ... " + req.url);
res.header('X-XSS-Protection', '0');
next();
}

app.post('/processLogIn', addToHeader, async (req, res) => {
console.log(req.headers)
res.send()
})

不幸的是,由于 X-XSS-Protection 没有出现在 header 中,问题仍然存在:

{ host: 'localhost:3000',
'user-agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:64.0) Gecko/20100101 Firefox/64.0',
accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'accept-language': 'en-US,en;q=0.5',
'accept-encoding': 'gzip, deflate',
referer: 'http://localhost:3000/logIn',
'content-type': 'application/x-www-form-urlencoded',
'content-length': '53',
dnt: '1',
connection: 'keep-alive',
cookie: 'io=ws5aQuqAjplpBAZyAAAA',
'upgrade-insecure-requests': '1',
'cache-control': 'max-age=0' }

根据文档和堆栈溢出,我将 res.header 更改为设置和追加,但它们给出了完全相同的结果。

此外,我已经正确安装和实现了快速 (v. ^4.16.3)

const express = require('express')
let app = express();

搭配套装:

addToHeader = function (req, res, next) {
console.log("add to header called ... " + req.url);
res.set('X-XSS-Protection', '0');
next();
}

输出:

{ host: 'localhost:3000',
'user-agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:64.0) Gecko/20100101 Firefox/64.0',
accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'accept-language': 'en-US,en;q=0.5',
'accept-encoding': 'gzip, deflate',
referer: 'http://localhost:3000/logIn',
'content-type': 'application/x-www-form-urlencoded',
'content-length': '53',
dnt: '1',
connection: 'keep-alive',
cookie: 'io=ws5aQuqAjplpBAZyAAAA',
'upgrade-insecure-requests': '1',
'cache-control': 'max-age=0' }

最佳答案

您使用的代码有误。使用 .set()

res.set('X-XSS-Protection', '0');

https://expressjs.com/en/4x/api.html#res.set

关于node.js - 无法在 express 中设置 HTTP header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53502313/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com