Click here to Skip to main content
15,747,908 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
how to send these 2 response for same single request.. when i tried the below code it shows an this error :-
(node:4148) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Cant set headers after they are sent.
(node:4148) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

if we are giving any one response at a time is working fine

What I have tried:

fs.readFile('public/html/sucess.html', function (err, data) {
      .then(function (buffer) {
         console.log('creating the file');
         res.setHeader('Content-Type', 'application/x-trt');
         res.setHeader('Content-Disposition', `inline; filename="${fileName}.trt"`);
         res.setHeader('Cache-Control', 'public, max-age=2592000'); // 30 days
         console.log(fileName +'.trt created');
Updated 16-Dec-17 4:44am
F-ES Sitecore 16-Dec-17 8:59am    
If you sent two responses to the browser what is it going to do with them? If I send 5 pages it can only show one so what happens to the others? The web is a request\response technology, you make a request you get a response.

If you want to download a file and also show a confirmation screen then you need to make two requests, and make sure the request for the file is directed to a new browser instance (target=_blank). Most browsers will recognise it's a file download and will show the download dialog rather than create a new tab. The other request is for your confirmation page and you'll make that a normal request.
veena nair 16-Dec-17 11:10am    
i am new to the programming world can you show me an example

1 solution

You can't. One request = One response.

If you're trying show a new page and download the file, the way to do it would be to have the new page make the request for the file, not the page you're leaving.
Share this answer
veena nair 16-Dec-17 11:12am    
i am new to this programming world ... can u show an example
Dave Kreskowiak 16-Dec-17 11:56am    
Nope. I don't have one ready.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900