實際上,restify
正在包裝幾個不同的包之一:spdy
,http
或https
。
if (options.spdy) {
this.spdy = true;
this.server = spdy.createServer(options.spdy);
} else if ((options.cert || options.certificate) && options.key) {
this.ca = options.ca;
this.certificate = options.certificate || options.cert;
this.key = options.key;
this.passphrase = options.passphrase || null;
this.secure = true;
this.server = https.createServer({
ca: self.ca,
cert: self.certificate,
key: self.key,
passphrase: self.passphrase,
rejectUnauthorized: options.rejectUnauthorized,
requestCert: options.requestCert,
ciphers: options.ciphers
});
} else if (options.httpsServerOptions) {
this.server = https.createServer(options.httpsServerOptions);
} else {
this.server = http.createServer();
}
來源:https://github.com/restify/node-restify/blob/5.x/lib/server.js
這些軟件包管理的要求,這是因爲內restify
事件處理的異步特性。 The EventListener calls all listeners synchronously in the order in which they were registered.。在這種情況下,restify
是監聽程序,並將按收到的順序處理請求。
縮放比例
話雖這麼說,像restify
web服務器往往通過釋放他們背後像nginx
代理多個進程放大。在這種情況下,nginx
將在進程之間拆分傳入請求,從而有效地允許Web服務器處理更大的併發負載。
Node.js的限制
最後,一定要記住,這是全部由Node.js的行爲限制由於應用程序在單個線程上運行,因此可以在執行緩慢的同步請求時有效地阻止所有請求。
server.get('/test', function(req, res, next) {
fs.readFileSync('something.txt', ...) // blocks the other requests until done
});
節點(在正常情況下)只在一個線程上運行應用程序,所以在單個回調中沒有併發問題。 –
@JoachimIsaksson這應該成爲一個答案。 – HeadCode
@HeadCode不是一個節點大師我希望別人做出更完整的答案,以便我沒有一些細節錯誤,我不知道那裏有一個:)沒有看到它一直沒有答案,所以我會盡量寫一些東西,當我一分鐘。 –