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

http://localhost:5000/attachments/1618413024408–1.jpg

Because, the file is located here enter image description here

But why can’t I access it?

Cannot GET /attachments/1618413024408--1.jpg

Answer

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