A link to the directory file.txt opens from the address localhost/files/testfolder/file.txt file.txt and a page opens with the address not localhost/files/testfolder/file.txt (as expected), but localhosy/files/file.txt . Why is this happening and how to fix it?

HTML file:

 <html> <head></head> <body> <h2>Files</h2> <a href="file.txt">Open file</a> </body> </html> 

Node JS server code

 app.get("/files/*", function(req, res) { path = req.params[0]; path = "files/" + path; try { var stats = fs.statSync(path); if (stats.isDirectory()) { fs.readdir(path, function(err, items) { res.render("files.ejs"); }); } } catch (e) { console.log("Error"); } }); 
  • one
    You didn’t add something in the second part, but as I understand, you don’t have an absolute link and it leads to the folder where you are - Artem Gorlachev
  • @ArtemGorlachev can you somehow do something to make the link tag just added to the url "/file.txt"? - MaximPixel
  • yes, but the path must be written from the root to the file - /files/testfolder/file.txt - Artem Gorlachev

1 answer 1

Redid it here and earned:

 <html> <head></head> <body> <h2>Files</h2> <a href="/openfolder/file.txt">Open file</a> </body> </html>