0
我正在嘗試從我的服務器API中獲取對象。在設置內容類型後發送它們後無法設置標題
此API將其作爲流。
我可以得到它的mime感謝它的關鍵(以.jpg結尾......)。 我想回發一個與正確的類型MIME內容類型,並不總是與application/octet-stream
。
我在故宮mime
該做的工作中發現,但是當我去到URL http://localhost:3000/file/myb/userId/test.jpeg
服務器關機,並拋出:
http.js:691
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.js:691:11)
at ServerResponse.res.setHeader (D:\Utilisateurs\A579871\Documents\GIT\shb- gitrepository-1\node_modules\express\node_modules\connect\lib\patch.js:63:22)
at ServerResponse.<anonymous> (D:\Utilisateurs\A579871\Documents\GIT\shb-gitrepository-1\node_modules\express\node_modules\connect\lib\patch.js:80:14)
at Array.forEach (native)
at ServerResponse.res.writeHead (D:\Utilisateurs\A579871\Documents\GIT\shb-gitrepository-1\node_modules\express\node_modules\connect\lib\patch.js:79:28)
at IncomingMessage.<anonymous> (D:\Utilisateurs\A579871\Documents\GIT\shb-gitrepository-1\server.js:49:17)
at IncomingMessage.EventEmitter.emit (events.js:95:17)
at IncomingMessage.<anonymous> (_stream_readable.js:746:14)
at IncomingMessage.EventEmitter.emit (events.js:92:17)
at emitReadable_ (_stream_readable.js:408:10)
我發現這個話題這個話題,並試圖使用建議仍然沒有找到解決方案
Error: Can't set headers after they are sent to the client
這裏是我的代碼:
var express = require('express');
var http = require('http');
var mime = require('mime');
var app = express();
var maxAge = 31557600000;
var port = 3000;
app.use(express.static(__dirname + '/app'));
app.get('/file/:bId/:userId/:fileKey', function(req, res) {
var headersGet = {
'auth-format': 'UID',
'Authorization': req.params.userId
};
var optionsGet = {
host: 'localhost',
port: 5000,
path: '/' + req.params.bId + '/' + req.params.fileKey,
method: 'GET',
headers: headersGet
};
var reqGet = http.request(optionsGet, function(resGet) {
resGet.on('data', function(d) {
type = mime.lookup(req.params.fileKey);
var charset = mime.charsets.lookup(type);
var newHead = {};
if (charset) {
newHead = {
"Content-Type": type
};
} else {
newHead = {
"Content-Type": type,
"charset": charset
};
}
res.writeContinue();
res.writeHead(200, newHead);
res.write(d);
res.end(d);
});
});
reqGet.end();
reqGet.on('error', function(e) {
console.error(e);
});
});
app.listen(port);
console.log('Listening on port ' + port);