0

I have a node app. Let's say the url is like this:

http://www.testurl.com/?access_token=1234. This url gets generated by Spotify as this access_token is required to use their API.

Only if the user refreshes the page I want the url to remove the last part of the url and just load http://www.testurl.com. It is important that it only happens on page refresh, so that the user has to authenticate themselves again.

I have tried these js solutions, but none of them work. I think they didn't work for the original poster also as there was no accepted answer.

I have seen that it is possible to detect browser refresh using php. Does anyone know if it is possible to detect the equivalent using node?

The php solution looks like this

$pageWasRefreshed = isset($_SERVER['HTTP_CACHE_CONTROL']) && $_SERVER['HTTP_CACHE_CONTROL'] === 'max-age=0';

I have also tried the following, that didn't work either.

window.onbeforeunload = function(event){
    window.location.href = 'http://www.google.com';
};
Community
  • 1
  • 1

2 Answers2

0

Why don't you instead just remove it from the url on page load, so that on page refresh it's gone?

history.replaceState(null, '', location.pathname);
Dominic
  • 62,658
  • 20
  • 139
  • 163
0

Don't know if that's the best way to solve whatever it is you're trying to do but those HTTP headers can be checked for inside Node by accessing request.headers object. Here's a sample code:

const http = require('http');
const url = require('url');

http.createServer(function(req, res) {
 res.writeHead(200, {
  'Content-Type': 'text/html'
 });

 // Home controller
 if(url.parse(req.url).path === "/") {

  if(req.headers["cache-control"] && req.headers["cache-control"] === "max-age=0") {
   console.log("User refreshed");
  } else {
   console.log("User did not refresh");
  }

  res.end("<h1>Hello there!</h1>");
 } else {
  res.statsCode = 404;
  res.end("Not found");
 }

}).listen(3000);
sangeeth96
  • 1,202
  • 1
  • 11
  • 20