You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Fastify服务器中如何获取请求来源域名?

在Fastify中获取请求来源域名的方法

和Express里通过req.get('origin')读取请求头的逻辑一致,Fastify中直接读取请求的Origin请求头即可:

fastify.get('/', (request, reply) => {
  const origin = request.headers.origin;
  // 使用origin做后续处理
  reply.send({ requestOrigin: origin });
});
  • 这里拿到的origin就是发起请求的来源域名,和Express中req.get('origin')返回的结果完全相同
  • 你之前尝试的request.id是Fastify生成的请求唯一标识,request.hostname是服务器自身的主机名,都不属于请求的来源域名范畴

内容的提问来源于stack exchange,提问作者umalogic

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 04:30:54