Last active
September 26, 2017 14:03
-
-
Save erfanio/0a8a94886f4aa6e20b5a568548d76b38 to your computer and use it in GitHub Desktop.
Send response in chunks
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 app = express(); | |
const wait = (sec) => new Promise((res, rej) => setTimeout(res, sec*1000)); | |
app.get('/yes', (req, res) => { | |
// 8 sec for header | |
wait(8) | |
// 8 * 2 = 16 sec for body | |
.then(() => { | |
res.write('Hello World!'); | |
return wait(2); | |
}) | |
.then(() => { | |
res.write('Hello World!'); | |
return wait(2); | |
}) | |
.then(() => { | |
res.write('Hello World!'); | |
return wait(2); | |
}) | |
.then(() => { | |
res.write('Hello World!'); | |
return wait(2); | |
}) | |
.then(() => { | |
res.write('Hello World!'); | |
return wait(2); | |
}) | |
.then(() => { | |
res.write('Hello World!'); | |
return wait(2); | |
}) | |
.then(() => { | |
res.write('Hello World!'); | |
return wait(2); | |
}) | |
.then(() => { | |
res.write('Hello World!'); | |
return wait(2); | |
}) | |
.then(() => { | |
res.end('Bye World!'); | |
}); | |
}); | |
app.get('/to-header', (req, res) => { | |
// 11 sec for header | |
wait(11) | |
// 8 * 2 = 16 sec for body | |
.then(() => { | |
res.write('Hello World!'); | |
return wait(2); | |
}) | |
.then(() => { | |
res.write('Hello World!'); | |
return wait(2); | |
}) | |
.then(() => { | |
res.write('Hello World!'); | |
return wait(2); | |
}) | |
.then(() => { | |
res.write('Hello World!'); | |
return wait(2); | |
}) | |
.then(() => { | |
res.write('Hello World!'); | |
return wait(2); | |
}) | |
.then(() => { | |
res.write('Hello World!'); | |
return wait(2); | |
}) | |
.then(() => { | |
res.write('Hello World!'); | |
return wait(2); | |
}) | |
.then(() => { | |
res.write('Hello World!'); | |
return wait(2); | |
}) | |
.then(() => { | |
res.end('Bye World!'); | |
}); | |
}); | |
app.get('/to-body', (req, res) => { | |
// 8 sec for header | |
wait(8) | |
// 11 * 2 = 22 sec for body | |
.then(() => { | |
res.write('Hello World!'); | |
return wait(2); | |
}) | |
.then(() => { | |
res.write('Hello World!'); | |
return wait(2); | |
}) | |
.then(() => { | |
res.write('Hello World!'); | |
return wait(2); | |
}) | |
.then(() => { | |
res.write('Hello World!'); | |
return wait(2); | |
}) | |
.then(() => { | |
res.write('Hello World!'); | |
return wait(2); | |
}) | |
.then(() => { | |
res.write('Hello World!'); | |
return wait(2); | |
}) | |
.then(() => { | |
res.write('Hello World!'); | |
return wait(2); | |
}) | |
.then(() => { | |
res.write('Hello World!'); | |
return wait(2); | |
}) | |
.then(() => { | |
res.write('Hello World!'); | |
return wait(2); | |
}) | |
.then(() => { | |
res.write('Hello World!'); | |
return wait(2); | |
}) | |
.then(() => { | |
res.write('Hello World!'); | |
return wait(2); | |
}) | |
.then(() => { | |
res.end('Bye World!'); | |
}); | |
}); | |
app.listen(3001, () => { | |
console.log('listening on port 3001!'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment