A simple node program with a single line of code quits immediately after running all the code:
console.log('hello');
However, an http server program listening on a port does not quit after executing all code:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World
');
}).listen(1337, '127.0.0.1');
So my question is, what made this difference? What made the first program quit after executing all code, while the second program continue to live?
I understand in Java, the specification says that when the last non daemon thread quits, the JVM quits. So, what is the mechanism in the nodejs world?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…