I'm trying to use a catch all api route to verify that a specific token is included in the header of the api call. Depending on the answer, I want to serve an html file from my directory based on the request url.
My code looks like this:
app.get('*', (req,res) => {
if (req.url == "/" ) {
res.sendFile(path.join(publicDir,'/login.html'));
} else {
console.log(path.join(publicDir, req.url));
res.sendFile(path.join(publicDir, req.url));
}
})
The first part of my loop works fine, when I go to localhost:4000/ I get redirected to my login.html page. From the login.html page I authorize the user and then make the following api call.
var req2 = new XMLHttpRequest();
req2.open("GET","http://localhost:4000/homepage.html",true);
req2.setRequestHeader("Authorization","Bearer +"+obj);
req2.send();
I've verified the request goes through correctly and gets caught in my catchall. The console.log statement produces the following: ~\public\homepage.html which is where my homepage.html file is stored. However, the homepage.html page is not actually served to the web browser. I can't figure out why, in the first part of my catchall, the sendFile function is correctly serving the login.html file but in the second part of my catchall, even though the console.log() is producing the exact correct path to my homepage.html file, the HTML is not actually getting served to the webpage.
Anyone have any ideas?