Skip to content Skip to sidebar Skip to footer

Node Js : Error With Res.download() After Res.render()

I'm beginning with Node JS, and I get an error : Error: Can't set headers after they are sent. You can see my code, the problem is with res.download(); Or, how can I show the vi

Solution 1:

You are sending res.download after res.render. this will try to send the response again, but you can't send response two times. That is what is causing the error Error: Can't set headers after they are sent.

What you need to do is render the view first( you can send a get request to render the view) and when that view is loaded, call another route to download the file( send post route to download)

app.get('/downloads', function(req, res) { 
    res.render('downloads.ejs');
});

app.post('/downloads', function (req,res){
    console.log("Python script begins");
    pythonShell.run('./generator.py', function (err) { 
        if (err) throw err; 
        console.log("Python Script Ended");
        res.download('mapCreated.tiff', 'map.tiff');
    }); 
})

Post a Comment for "Node Js : Error With Res.download() After Res.render()"