Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
galaxine-senpai opened this issue Apr 11, 2024 · 2 comments
Closed

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

galaxine-senpai opened this issue Apr 11, 2024 · 2 comments
Labels

Comments

@galaxine-senpai
Copy link

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
Copy link
Contributor

krzysdz commented Apr 11, 2024

__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
Copy link
Member

Yep, its a path construction issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants