Last active
August 29, 2015 14:13
-
-
Save gabhi/4cf34e2598d0e335c5cd to your computer and use it in GitHub Desktop.
node js unzip request example
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') | |
var app = express() | |
app.use(function(req, res, next) { | |
res.header("Access-Control-Allow-Origin", "*"); | |
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); | |
next(); | |
}); | |
app.get('/', function(req, res) { | |
var zlib = require('zlib'); | |
var http = require('http'); | |
var fs = require('fs'); | |
var request = http.get({ | |
host: 'api.walmartlabs.com', | |
path: '/v1/feeds/bestsellers?apikey=a6kwenh4cpva3tzt7wdvwb7r', | |
port: 80, | |
headers: { | |
'accept-encoding': 'gzip,deflate' | |
} | |
}); | |
request.on('response', function(response) { | |
switch (response.headers['content-encoding']) { | |
// or, just use zlib.createUnzip() to handle both cases | |
case 'gzip': | |
console.log("gzip"); | |
response.pipe(zlib.createGunzip()).pipe(res); | |
break; | |
case 'deflate': | |
response.pipe(zlib.createInflate()).pipe(res); | |
break; | |
default: | |
response.pipe(res); | |
break; | |
} | |
}); | |
}) | |
var server = app.listen(3000, function() { | |
var host = server.address().address | |
var port = server.address().port | |
console.log('Example app listening at http://%s:%s', host, port) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nicely done!