Skip to content

Instantly share code, notes, and snippets.

@shuhei

shuhei/index.js

Created Mar 2, 2019
Embed
What would you like to do?
Callback of ClientRequest.prototype.end
const http = require('http');
const server = http.createServer((req, res) => {
console.log('[server] request');
setTimeout(() => {
res.end('hello');
console.log('[server] response');
}, 3000);
});
server.listen(3000, () => {
startClient();
});
function startClient() {
const req = http.request({
hostname: '127.0.0.1',
port: 3000,
});
req.on('response', (res) => {
console.log('[client] response');
res.on('end', () => {
console.log('[client] response end');
});
res.resume();
server.close(() => {
console.log('[client] server close');
});
});
req.end(() => {
console.log('[client] request finish');
});
console.log('[client] request start');
}
@shuhei

This comment has been minimized.

Copy link
Owner Author

@shuhei shuhei commented Mar 2, 2019

[client] request start
[client] request finish
[server] request
[server] response
[client] response
[client] server close
[client] response end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.