0

i'm building this application using node and express js, on the front end i have an anchor element like this:

<a href="/tutorials/tut1" target="_blank" id="tutorialLink">tut 1</a>

now this opens and works perfectly on localhost, but when deployed i get cannot Get error. it's supposed to serve a static HTML file that is stored in 'tutorials' directory. i tried doing this

app.use("/tutorials", express.static('tutorials'));

and this (with all kinds of path formatting):

app.get("/tutorials/:tutname", (req, res) => {
  let tutname=req.params.tutname;
  res.sendFile(__dirname+`/tutorials/${tutname}.html`); //also tried res.sendFile(`${tutname}.html`,{root:__dirname});
})

both those ways worked perfectly on local host, but as i said above when i deploy i get "Cannot GET /tutorials/tut1" error

my app is deployed to a digitalocean vps and i'm using nginx on it, i should mention that serving other static files/images/scripts works perfectly with no problems as well

M.KH
  • 388
  • 1
  • 5
  • 20
  • check [this](https://stackoverflow.com/a/25463996/7574023) out – boolfalse Apr 11 '21 at 17:19
  • i saw this while i was searching for a solution, didn't benefit me – M.KH Apr 11 '21 at 17:26
  • Is the 404 served by nginx or your app? If you add a middleware before express.static which just `console.log`s the `req.path` and then calls `next()`, do you see the middleware being called when you go to that address? Have you tried replacing `express.static('tutorials')` with `express.static(path.join(__dirname, 'tutorials'))`? – cbr Apr 11 '21 at 17:32
  • i don't know how to view console.logs on an ubunto while the server is running as a background service, but again everything is working well on local host, i tried the line you posted and tried it and it works on local host but not on the deployed site.. – M.KH Apr 12 '21 at 11:14
  • did you find an answer to this? – Rice Junkie Jun 09 '22 at 22:42
  • @RiceJunkie yeah i forgot to restart my node.js app after adding changes to it, restarting the server fixes the problem – M.KH Aug 02 '22 at 14:14

0 Answers0