简体   繁体   中英

Can't set headers after they are sent after setting the content-type

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);

The major problem here is that you must not assume you will always get a single 'data' event. Instead, write the header once (use a boolean guard variable if you want to do this in a 'data' event so that it only gets done once), write the data, and on resGet 's 'end' event, end the response.

OR: write the header (outside of the 'data' event) and just resGet.pipe(res); , removing the resGet 's 'data' and 'end' event handlers entirely.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM