Created
January 25, 2018 07:40
-
-
Save gabemeola/a8ce233252742e56c5723cddb11ef5a5 to your computer and use it in GitHub Desktop.
Slow-mo Nodejs Chunk Encoding Test
This file contains hidden or 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 PORT = process.env.PORT || 8080 | |
http.createServer((request, response) => { | |
response.setHeader('Content-Type', 'text/html; charset=UTF-8'); | |
response.setHeader('Transfer-Encoding', 'chunked'); | |
response.write(( | |
'<!DOCTYPE html>' + | |
'<html lang="en">' + | |
'<head>' + | |
'<meta charset="utf-8">' + | |
'<title>Chunked transfer encoding test</title>' + | |
'</head>' + | |
'<body>' | |
)); | |
response.write('<h1>Chunked transfer encoding test</h1>'); | |
// Now imitate a long request which lasts 5 seconds. | |
setTimeout(() => { | |
const html = '<h5>This is a chunked response after 5 seconds. The server should not close the stream before all chunks are sent to a client.</h5>' | |
response.write(html); | |
// since this is the last chunk, close the stream. | |
response.end('</body></html>'); | |
}, 5000); | |
// this is another chunk of data sent to a client after 2 seconds before the | |
// 5-second chunk is sent. | |
setTimeout(() => { | |
const html = '<h5>This is a chunked response after 2 seconds. Should be displayed before 5-second chunk arrives.</h5>' | |
response.write(html); | |
}, 2000); | |
}).listen(PORT, () => console.log(`App up at http://localhost:${PORT}`)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment