expressjs / express

Fast, unopinionated, minimalist web framework for node.
https://expressjs.com
MIT License
65.62k stars 16.22k forks source link

.get() attempting to grab from home dir (Linux) #5592

Closed galaxine-senpai closed 7 months ago

galaxine-senpai commented 7 months ago

OS: Debian GNU/Linux 11 (bullseye) aarch64 Bug: .get() (specifically when using robotx.txt) trys to grab /home/xxxxx/microwavebot-webrobots.txt (I presume due to the folder name)

Code snippet:

app.get("/robots.txt", function (req, res) {
  res.sendFile(__dirname + "robots.txt");
});

Expected result: It grabs robots.txt from the current directory Actual result: it attemps to grab from the home directory of the user

Note: I am entirely unsure if this is due to something I did that I don't remember or my OS or maybe even an actual bug

krzysdz commented 7 months ago

__dirname does not have trailing slash, so your problem is the string concatenation.

You can use path.join, which will build a path from segments by inserting platform-specific directory separator between the segments.

// const path = require("node:path");
app.get("/robots.txt", function (req, res) {
    res.sendFile(path.join(__dirname, "robots.txt"));
});

__dirname and path.join usage demonstration

jonchurch commented 7 months ago

Yep, its a path construction issue.