I am trying to get an object from my server API.
This API has it as a stream.
I can get its mime thanks to its key (which ends by .jpg etc...).
I would like to send back a content-type with the right type-mime and not always with application/octet-stream
.
I found in npm mime
which does the work but when I go to the url http://localhost:3000/file/myb/userId/test.jpeg
the server shutdown and throw :
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)
I found this topic about the subject and tried to use the advices without finding the solution
Error: Can't set headers after they are sent to the client
Here is my code :
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);