Created
May 2, 2018 05:02
-
-
Save aichholzer/09c55e55c05084e2c7626a0e3178af29 to your computer and use it in GitHub Desktop.
Gzip - client/server
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 http = require('http'); | |
const { readFile } = require('fs'); | |
const { gzip } = require('zlib'); | |
const { promisify } = require('util'); | |
const run = async () => { | |
try { | |
const data = await promisify(readFile)('./menu.json'); | |
const req = http.request({ | |
method: 'POST', | |
hostname: '127.0.0.1', | |
port: 9292, | |
path: '/' | |
}, (res) => { | |
res.on('data', (response) => { | |
console.log(response.toString()); | |
}); | |
}); | |
req.write(await promisify(gzip)(data)); | |
req.end(); | |
} catch (error) { | |
console.error(error); | |
} | |
}; | |
run(); |
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 { createGunzip } = require('zlib'); | |
const app = express(); | |
app.post('/', (req, res) => { | |
const buffer = []; | |
const gunzip = createGunzip(); | |
req.pipe(gunzip); | |
gunzip | |
.on('data', (data) => buffer.push(data)) | |
.on('end', () => { | |
// const result = buffer.join(''); | |
res.send('Request processed.'); | |
}).on('error', (error) => res.send(`Error: ${error.message}`)); | |
}); | |
app.listen(9292, () => console.log('Up: http://localhost:9292')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment