How to block audience?

Hi,

Is there any way that my page can only be viewed in Canada?

If you are using Node.js (you have to make your site dynamic for something like this to work) you can use the code below which gets the users country based on their ip address with ipwhois.

// NOTE: make sure you have node-fetch and express imported

app.get("/", (request, response) => {

  // get user ip
  var ip = request.header("x-forwarded-for").split(",")[0] || request.connection.remoteAddress;

  // get ip info from api
  fetch("http://ipwhois.app/json/" + ip)
    .then(res => res.json())
    .then(json => {

    // block requests not from canada
    if (json.country_code !== "CA") return response.end(403)

    // ...

  });
});

2 Likes

Thank you, I will try it out. It will only work for Node.js?

You can use the ipwhois API in any language with an HTTP client.