Created
August 12, 2021 20:46
-
-
Save sfoster/61a68e7106139a48362eeb353aa4a0c4 to your computer and use it in GitHub Desktop.
Server to throttle response to a .zip file request so you can easily work on downloads without filling up your disk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const cwd = process.cwd(); | |
const path = require('path'); | |
const docRoot = cwd; | |
const app = express(); | |
const port = 3000; | |
const fs = require('fs'); | |
const Throttle = require('stream-throttle').Throttle; | |
app.get('/*.zip', async function(request, response, next) { | |
const url = new URL(request.url, `http://${request.headers.host}`); | |
let pathname = path.join(docRoot, url.pathname); | |
const dirname = path.dirname(pathname); | |
const fileName = "download.jpg"; | |
console.log("Handling request for: ", url, pathname); | |
let stat = fs.statSync(pathname); | |
response.writeHead(200, { | |
"Content-Type": "application/octet-stream", | |
"Content-Disposition": "attachment; filename=" + fileName, | |
// 'Content-Length': stat.size | |
}); | |
fs.createReadStream(pathname).pipe(new Throttle({ rate: 1000 })).pipe(response); | |
}); | |
app.listen(port, () => { | |
console.log(`Slow download app listening at http://localhost:${port}`) | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment