2

I'm using Node.js+Express, and I've come across a case where I need to send some HTML, render a view, then send some more HTML in the one response.

The flow would be:

res.send('some html');
res.render('module.html', {});
res.send('more html');

Now I know that res.render supports a callback, so I could do:

res.render('module.html', {}, function () {
    res.send('more html');
});

But res.send() doesn't appear to. Is there a way to achieve this?

1
  • 2
    I would recommend injecting a rendered partial view into your html and sending that. Commented Jun 29, 2011 at 11:32

1 Answer 1

10

Like Raynos Said I would recommend looking into view partials. This video from Author TJ explains the basics.

As a sidenote res.send send a full response and then closes the connection. You can not be using that if you want to send more text after that.enter image description here


So you have to use node.js native res.write to do what you want instead.

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

1 Comment

Thanks Alfred, I don't know why I didn't think to use res.write instead. Make sense. I didn't want to use partials particularly as it is literally two additional tags I want to wrap the view around. I thought it might be a bit overkill for partials.

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.