Node - Tell origin of request


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?

For example, I would like to serve a web page when someone visits my URL directly (types in in a web browser, but when a user calls a resource on my app via a url from a seperate domain (like <img src='' />) 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

Problem courtesy of: Joe Longstreet


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, '');

edited the answer to reflect the input from anu below - it should be referer

Solution courtesy of: Gates VP


In middleware you have to use this way "req.headers.origin"

app.use(function(req, res, next) {
    //var origin=req.headers.origin
Discussion courtesy of: Imdadul Huq Naim

This recipe can be found in it's original form on Stack Over Flow.