0

using the values submitted by a webfrom served by express to initiate crawl using request + cheerio.

the parts work separately, when put together i get an error: Can't set headers after they are sent.

what's wrong here?

router.post('/', function(req, res){

    res.json('processing')        

    crawl(r.keyword, r.tld, r.brand).then(function(d){

       userObject.urlCache = d;

     }) 
})
  • the error mean that the response is sent twice. The reason is not clear with this code. Do you run post request treatments ? Can you also carefully bind all error handlers ? http://odetocode.com/blogs/scott/archive/2015/10/01/javascript-promises-and-error-handling.aspx –  May 10 '16 at 12:27

2 Answers2

0

res.json('processing') will send a response. You cannot modify the headers after sending a response. It is not clear from you code snippet what is modifying the response after it is being sent.

For more info on the express response object: http://expressjs.com/en/api.html#res.json

Also there are more response options than res.json. You are not really sending json in your example. You might want res.send('processing'), but that will not fix your set header issue..

0

You should set the header first and then send the request out.

res.setHeader('charset', 'utf-8');
res.send('processing');
Anthony C
  • 2,117
  • 2
  • 15
  • 26