Skip to content

Instantly share code, notes, and snippets.

@davidbanham
Created September 1, 2011 12:02
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save davidbanham/1186032 to your computer and use it in GitHub Desktop.
Save davidbanham/1186032 to your computer and use it in GitHub Desktop.
Dynamically generate a file download in express without touching the filesystem
var express = require('express');
var app = require('express').createServer();
app.listen('3030');
app.get('/', function(req, res) {
res.contentType('text/plain');
res.send('This is the content', { 'Content-Disposition': 'attachment; filename=name.txt' });
});
@rogeriochaves
Copy link

rogeriochaves commented Apr 30, 2020

saved my life

@bre7
Copy link

bre7 commented Aug 8, 2020

Express 4:

app.get('/', async (req, res) => {
    res.status(200)
        .attachment(`name.txt`)
        .send('This is the content')
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment