Created
March 1, 2018 03:19
-
-
Save bbstilson/e0afc32282ad4b90a27a3eba634211fd to your computer and use it in GitHub Desktop.
Streaming a file from one node server to another
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
// transmitter.js | |
const http = require('http'); | |
const fs = require('fs'); | |
const requestOpts = { | |
hostname: 'localhost', | |
port: 1337, | |
method: 'POST' | |
}; | |
const readStream = fs.createReadStream('./s1/lorem.txt'); | |
const request = http.request(requestOpts); | |
readStream.pipe(request); | |
request.on('response', ({ statusCode, statusMessage }) => { | |
console.log(`${statusCode} ${statusMessage}`); | |
}); | |
//////////////////////////////////////////////////////////////////////// | |
//////////////////////////////////////////////////////////////////////// | |
// server.js | |
const http = require('http'); | |
const fs = require('fs'); | |
http.createServer((request, response) => { | |
const writeStream = fs.createWriteStream('./s2/lorem.txt'); | |
request.pipe(writeStream); | |
request.on('end', () => { | |
writeStream.close(); | |
response.writeHead(200); | |
response.end('OK'); | |
}); | |
}).listen(1337, () => { console.log('Listening on port 1337') }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment