0

sorry for my english, im trying to uso log4js for set a log on the api calls in express, im stuck with this, when try to log the patition.

http_outgoing.js:503
    throw new errors.Error('ERR_HTTP_HEADERS_SENT', 'set');
    ^

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at validateHeader (_http_outgoing.js:503:11)
    at ServerResponse.setHeader (_http_outgoing.js:510:3)
    at ServerResponse.header (/src/react/impex/jumbo-react-flat/apilogica/node_modules/express/lib/response.js:767:10)
    at ServerResponse.send (/src/react/impex/jumbo-react-flat/apilogica/node_modules/express/lib/response.js:170:12)
    at ServerResponse.json (/src/react/impex/jumbo-react-flat/apilogica/node_modules/express/lib/response.js:267:15)
    at Request.request.get [as _callback] (/src/react/impex/jumbo-react-flat/apilogica/controllers/database.js:62:40)
    at self.callback (/src/react/impex/jumbo-react-flat/apilogica/node_modules/request/request.js:185:22)
    at Request.emit (events.js:180:13)
    at Request.emit (domain.js:422:20)
    at Request.onRequestError (/src/react/impex/jumbo-react-flat/apilogica/node_modules/request/request.js:881:8)

this is my log function with log4js, it receive the request object, an get some differents params for the log action

var logger = require('log4js');


exports.logapi = function (service,vars) {
  var ip = vars.connection.remoteAddress;
  debugger;
  logger.configure({
    appenders: { 
      out:{type:'console'},
      access: { type:'dateFile', filename: '../logs/access.log', category:'info'  } ,
      error: { type:'dateFile', filename: '../logs/error.log', category:'error' },

    }
  });

  log = logger.getLogger('access');
console.log(vars.query.canal);
  log.info('Servicio:'+service+ '; Fecha:' + Date.now() +'; Canal:'+vars.query.canal+'; IP'+ip+'; Usuario:'+vars.query.user+' token:'+vars.query.token);


  return;
}

this is how im calling the log function when an api function get a petition, this is another module function

var apicall = require('./log');
exports.getproduct = function (req, res) {
    res.header('Content-Type', 'application/json');
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Headers', 'Content-Type');
var reqlog=req;
apicall.logapi('setnewproduct',reqlog);

   request.get({
        "headers": { "content-type": "application/json" },
        "url": "http://localhost:3001/controller_producto/productos/"+req.params.id
    }, (error, response, body) => {
        if(error) {
            return res.sendStatus(400).json({
                error: 'Que verguenza! Ocurrio un error, intentalo nuevamente en unos minutos'
              })
        }else{

            return res.send(body);}

    });


};

1 Answer 1

1

Yes, the problem, is that in Express, you cannot set the headers after you send a response to the client, and in this instance, you need to defer to the default errorhandler. See:

https://expressjs.com/en/guide/error-handling.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.