Last active
September 29, 2016 13:26
-
-
Save meiriko/e5b952848bbc071c7e8af6714f7ac66a to your computer and use it in GitHub Desktop.
Download external zip file via node for Omer
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
<!-- IMPORTANT. Put me under the /static dir, gist has flat files --> | |
<html> | |
<head> | |
</head> | |
<body> | |
<div>dl example</div> | |
<div><a href="/dlzip/http://www.7-zip.org/a/7za920.zip">dl: http://www.7-zip.org/a/7za920.zip via server</a></div> | |
<div><button onClick="goto('/dlzip/http://www.7-zip.org/a/7za920.zip')">navigate by click (js)</button></div> | |
<script> | |
function goto(url){ | |
window.location.href = url; | |
} | |
</script> | |
</body> | |
</html> |
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
var express = require('express'), | |
request = require('request') | |
bodyParser = require('body-parser'); | |
var app = express(); | |
app.use(bodyParser.json()); | |
app.use(bodyParser.urlencoded({ extended: false })); | |
app.get(/dlzip\/.*/, function(req, res){ | |
var zipUrl = req.path.replace('/dlzip/',''); | |
var fileName = zipUrl.split('/').pop(); | |
res.set({ | |
"Content-Disposition": 'attachment; filename="'+fileName+'"' | |
}); | |
request(zipUrl).pipe(res); | |
}); | |
app.use('/', express.static(__dirname + '/static')); | |
var port = process.env.PORT || 8080; | |
var ipaddress = process.env.IPADDR || '0.0.0.0'; | |
app.listen(port, ipaddress, function () { | |
console.log('%s: Node server started on %s:%d ...', | |
Date(Date.now()), ipaddress, port); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment