node.js - 无法在 express 中设置响应标头
问题描述
我无法设置我的标题有什么问题?
const express = require('express');
const app = express();
app.get('/', (req,res)=>{
res.header('myHeader','ok');
/*
even
res.set('myHeader','ok');
tried but there is no difference
*/
res.redirect('/x');
})
app.get('/x',(req,res)=>{
res.send(req.headers);
})
app.listen(3000);
当我导航到时,localhost:3000
每次都会得到相同的结果。
这是结果:
{
host: "localhost:3000",
connection: "keep-alive",
upgrade-insecure-requests: "1",
user-agent: "XX",
accept: "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
accept-encoding: "gzip, deflate, br",
accept-language: "en-US,en;q=0.9",
cookie: "XX"
}
如您所见myHeader
,它没有出现在列表中。
解决方案
好的,这就是解决方案,当我们进行重定向时,实际上我们正在发出一个新的 http 请求。所以在我的情况下,路径req
中使用的/x
路径与路径不同/
。
推荐阅读
- apache-camel - 服务器模式下的 Apache Camel netty4 生产者
- android - 通过 Jenkins 为 gradle glibc 2.14 导出 LD_LIBRARY_PATH 不起作用
- javascript - 为特定行和特定列中的单元格停用点击事件
- javascript - Multiple pie charts in 1 graph with split tooltip Highcharts.js
- android - 我已经使用 Io.card sdk 完成了信用卡扫描,但我无法找到扫描借记卡的解决方案
- python - 如何从 HTML 获取 href 链接并使用 Python 下载文件?
- python - 如何在加密代码中解密文件
- jquery - 弹出 Div td 使用 Jquery 获取值
- docker - Docker zeppelin + Docker openresty
- mysql - 日期作为 birt 请求中的参数