Home > Software design >  express downloading files with no filename and only an extension like ".gitignore" leads t
express downloading files with no filename and only an extension like ".gitignore" leads t

Time:01-21

I am writing a simple file server to upload and download files with [email protected], and Im stuck on the download part. Whenever I try to download a file that has no name, but only an extension (like .gitignore or .prettierrc) with res.download, I get a NotFoundError (see full error message below). Example:

app.get("/download", function (req: Request, res: Response) {
    res.download("C:/Users/stadl/Desktop/File-Server/.gitignore");
});

results in a NotFoundError, however, the following works:

app.get("/download", function (req: Request, res: Response) {
    res.download("C:/Users/stadl/Desktop/File-Server/t.txt");
});

The file paths are both correct, and both files are existing, the directory structure looks like that:

C:/Users/stadl/Desktop/File-Server/
|
|
| - - - client/
| - - - server/
|          |
|          | - - - server.ts (contains the app.get("/download")... snippet)
|          . . .
| 
| - - - .gitignore
| - - - t.txt
|
. . .
 

The same happens with relative paths, (../.gitignore and ../t.txt). Is this a bug, is this intended or am I doing something wrong?

CodePudding user response:

You need to allow dotfiles in the options object of res.download by setting allow, since it's disabled/ignored by default (the default value ignore sends 404):

res.download("C:/Users/stadl/Desktop/File-Server/t.txt", {dotfiles:"allow"});

see:

dotfiles
Option for serving dotfiles. Possible values are “allow”, “deny”, “ignore”.

https://expressjs.com/en/5x/api.html#res.download

and

dotfiles
Possible values for this option are:

“allow” - No special treatment for dotfiles.
“deny” - Deny a request for a dotfile, respond with 403, then call next().
“ignore” - Act as if the dotfile does not exist, respond with 404, then call next().

https://expressjs.com/en/5x/api.html#express.static

  • Related