[Fixed] How can I allow a user to download a file from a link in express?


I have a function that allows you to create a folder in the directory where the server is located, and upload files there
And I’m trying to access them via a direct link, like this


Because, the file is located here
enter image description here

But why can’t I access it?

Cannot GET /attachments/1618413024408--1.jpg


Express itself provides an easy to use implementation for this express.static(root, [options]).

Simply add this add the right position in your code:

app.use('/attachments', express.static(path.join(__dirname, 'attachments')))

Make sure that path.join(__dirname, 'attachments') points to the right directory (with a simple console.log) otherwise just adjust it.

Documentation: https://expressjs.com/en/starter/static-files.html

Leave a Reply

(*) Required, Your email will not be published