Skip to content

Instantly share code, notes, and snippets.

@aichholzer
Created May 2, 2018 05:02
Show Gist options
  • Save aichholzer/09c55e55c05084e2c7626a0e3178af29 to your computer and use it in GitHub Desktop.
Save aichholzer/09c55e55c05084e2c7626a0e3178af29 to your computer and use it in GitHub Desktop.
Gzip - client/server
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();
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