你可以做res.json()
只有一次。考慮下面這個例子:
$ curl http://localhost:3333/json1
你2秒後得到這樣的::
var express = require('express');
var app = express();
app.get('/json1', function (req, res) {
setTimeout(function() {
res.json({ok:true});
}, 2000);
});
app.get('/json2', function (req, res) {
setTimeout(function() {
res.json({ok:true});
res.json({ok:true});
}, 2000);
});
app.listen(3333);
當你訪問它
{"ok":true}
但是,如果你試圖用訪問:
curl http://localhost:3333/json2
然後你仍然得到這個在你的c lient方:
{"ok":true}
但是你的服務器崩潰,並:
_http_outgoing.js:344
throw new Error('Can\'t set headers after they are sent.');
^
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:344:11)
這意味着,快遞等待res.json()
,一旦它得到一個完成的請求,但在那之後,你不能把它第二次。
與res.send()
相同 - 例如,看到那些路線:
app.get('/send1', function (req, res) {
setTimeout(function() {
res.send('ok');
}, 2000);
});
app.get('/send2', function (req, res) {
setTimeout(function() {
res.send('ok');
res.send('ok');
}, 2000);
});
在另一方面,似乎你可以調用res.end()
兩次,第二呼叫被忽略:
app.get('/end1', function (req, res) {
setTimeout(function() {
res.end('ok');
}, 2000);
});
app.get('/end2', function (req, res) {
setTimeout(function() {
res.end('ok');
res.end('ok');
}, 2000);
});
但是如果你使用的res.write()
代替res.end()
那麼請求將等待res.end()
和寫不完:
app.get('/end1', function (req, res) {
setTimeout(function() {
res.end('ok');
}, 2000);
});
app.get('/end2', function (req, res) {
setTimeout(function() {
res.end('ok');
res.end('ok');
}, 2000);
});
但該消息實際上被交付 - 其中Y OU可以通過「\ n」結尾的消息觀察,使curl
顯示它時,它到達:
app.get('/write1', function (req, res) {
setTimeout(function() {
res.write('ok\n');
}, 2000);
});
app.get('/write2', function (req, res) {
setTimeout(function() {
res.write('ok\n');
res.write('ok\n');
}, 2000);
});
因此,大家可以看到,有一定的方式發送數據 - 像res.write()
,可多次使用並沒有關閉連接。還有其他方式,如res.json()
只能使用一次,並且它們隱式關閉連接。
但是,如果你添加的路由像這樣的:
app.get('/empty', function (req, res) {
});
然後快速將永遠開放的連接等待,因爲它沒有辦法知道是否res.end()
或res.json()
將在未來被調用。它只能知道它是否已被調用。
所以基本上,連接保持打開狀態,直到你在'response'上發送''或'json'的東西或超時。 –
@CallumLinington是的。但是對於超時(在Express方面),你必須使用一個像'connect-timeout'這樣的中間件,其中'var timeout = require('connect-timeout'); app.use(timeout('5s'));'或者類似的東西。否則Express將等待客戶端的超時或TCP連接的超時。 – rsp
哦,對,那是一顆寶石! –