Created
December 26, 2016 06:39
-
-
Save mooyoul/a36aeeebb75cf4a32fce7d714ce7f96e to your computer and use it in GitHub Desktop.
Node.js blocking event loop example
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
'use strict'; | |
const http = require('http'); | |
const server = http.createServer((req,res) => { | |
res.writeHead(200, {'Content-Type': 'text/plain'}); | |
res.end('ok'); | |
// below code will block everything until 10 seconds elapsed | |
console.log('starting blocking code, try make connection!'); | |
const target = Date.now() + 1000 * 10; | |
while(Date.now() < target) { | |
} | |
console.log('ended blocking code, re-try make connection :)'); | |
}); | |
server.listen(3000); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment