Download a file from a beaglebone web server using client side <a href="filename" download> </a>

I have a web server running on a BBB and serving an html page with some tabular data and control buttons. On demand, I would like to download a file of the tabular data to the client. I can create, write, and delete the file with the fs module.

In the web page, I have HTML like Click to download.

When I click, I get a download file named “Sample.txt” but the contents of the file are the HTML code of the web page. I think this is because I am not handling the download command in the server side JavaScript.

Can someone explain what JavaScript should be in the server to catch the ‘download’ command and return the specified file? What should the “socket.on(???, downloadFile);” look like? What’s the correct way to return the file so the client browser correctly saves it?

I can provide the rest of the code if needed, but it seems to be working - I just can’t find references to handling the file download from that element.

Thanks.

It looks much the same as what it does to display your index page, but
instead of setting "Content-Type: text/html" and dumping HTML, it sets
"Content-Type: application/octet-stream" and dumps binary data.

You haven't told us what HTTP server you're using, so it's a little
difficult to say exactly how you do it. One commonly used one for this
task is Express…

https://expressjs.com/en/starter/hello-world.html gives an example of
doing more or less what you've done… if you were to modify that front
page with a hyperlink, the extra code would look like this:

app.get('/mydata.csv', function (req, res) {
  res.set('Content-Type', 'application/octet-stream');
  res.send('your CSV data');
});