0

I am working on a node.js and express.js app.

I have router configured as:

var express = require('express');
var router = express.Router();

router.get('/', function(req, res, next) {

   // on load

   res.render('mail', { title: "this is the title." });

   // after some more funcitonality redirect/render a new view

 If(condition == 'success'){

   res.render('sent', { title: "this is the   title." });

   // or

   res.redirect('/sent');

  }


});

Now the problem is when I render a new view based on the condition after rendering the first view or if I redirect to a new route after rendering the first view, I get

Headers cannot be set after they have been sent error

The execution though continues but the view is not loaded and the page is not redirected.

Any help to overcome this issue will be much appreciated.

2
  • You can't redirect after you have already sent a view (page) Commented Aug 8, 2017 at 21:49
  • What are you trying to do here? Show a mail page and then change that to a 'sent' page once the message is sent? Commented Aug 8, 2017 at 21:51

1 Answer 1

1

You can't send a second response.

Or you render a view or redirect to another route, not both.

Look in express for the documentation.

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

1 Comment

Quite illogical i got. 😊 i have hadled it through ajax calls to carry out functions and return to the same view and than redirect.

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.