节点 - 告诉请求的来源 [英] Node - Tell origin of request

查看:14
本文介绍了节点 - 告诉请求的来源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以区分直接来自浏览器中的 URL 的请求与从远程网页调用的资源之间的区别?

Is it possible to tell the difference between a request coming directly from a URL in a browser vs. a resource being called from a remote web page?

例如,当有人直接访问我的 URL 时,我想提供一个网页(输入 http://mywebsite.com) 在网络浏览器中,但是当用户通过来自单独域的 url 调用我的应用程序上的资源时(如 <img src='http://mywebsite.com'/>) 那么我想提供不同的内容.

For example, I would like to serve a web page when someone visits my URL directly (types in http://mywebsite.com) in a web browser, but when a user calls a resource on my app via a url from a seperate domain (like <img src='http://mywebsite.com' />) then I'd like to serve different content.

我查看了请求和标头,但不管怎样,它看起来都一样

I've looked in the request and in the headers but it looks the same regardless of

推荐答案

我认为您正在寻找 request.header 中的 referer 字符串.

I think you are looking for the referer string in the request.header.

所以简单版本看起来像这样:

So the simple version would look like this:

http.createServer(function (req, res) {
  var ref = req.headers.referer;

  if(ref) {
    // serve special content
  }
  else {
    // serve regular homepage
  }
}).listen(1337, '127.0.0.1');

<小时>

编辑了答案以反映下面来自 anu 的输入 - 应该是 referer

这篇关于节点 - 告诉请求的来源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆