3

I am running a node.js server, and it is rendering a web page wonderfully. When I look at this in a browser, it runs exactly as I expect.

However, what I actually want to do is make the call to fully generate the html page - exactly as it is in the browser - within the node.js code, as a call. Currently, I have tried this:

http.request("http://localhost:8000/").end();

(with a few variants). This does exactly what it says, which is to make the single call to the server for the page - what it doesn't do is actually render the page, pulling in all of the other script files, and running the code on the page.

I have tried exploring express and ejs, and I think I need to use one of these, but I cannot find out how to do this fairly straightforward task. All it needs is to render an html page, but it seems to be a whole lot more complex than it should be.

8
  • 1
    "All it needs is to render an html page, but it seems to be a whole lot more complex than it should be." -- I think your expectations are way off, rendering a web page is an incredibly complex process. Try searching for "mechanize-js" and "zombie" to get an idea of the complexity. Commented Aug 29, 2013 at 17:08
  • Nodejs isn't a browser. What exactly do you want to do? Are you looking to load a remote page and send to connecting browsers, Like a proxy? Commented Aug 29, 2013 at 17:18
  • 1
    I am aware that a browser is complex - what I meant is that the idea of simply rendering a page feels like it should be easy. Yes, I know nodejs isn't a browser, I am looking for something like a proxy, to render pages on the server, to send finished pages to the client. All of the evidence does indicate that this seems like a thing to do. In essence, I want to call up www.google.com (not actually, but for example), and get the page to render fully as if I were calling from a browser. That is all (!!). It seems like something that some node developer should have answered somewhere! Commented Aug 29, 2013 at 17:20
  • So are you looking to create a proxy or actually implement a web browser on the server? Commented Aug 29, 2013 at 17:21
  • I am looking to render a page on the server, the results of which I will then use. Commented Aug 29, 2013 at 17:26

3 Answers 3

1

What output do you want? A string of HTML? Maybe you want PhantomJS the headless browser. You could use it to render the page, then get the rendered DOM as a string of HTML.

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

3 Comments

This was the closest I got to making it work. In the end, we went down a different route - equally crazy, but seems to be working better. I don't make the rules, sadly.
@SchroedingersCat what did you end up doing?
@Alex - I split the processing to keep the rendering on the browser, and the processing that I could on the server.
0

Use the Mikeal's Request module to make http requests once you captured the response you then can inspect the html however you like.

To make that easier though you should use cheerio, this will give you a jQuery style api to manipulate the html.

Comments

0

Perhaps you are looking for wkhtmltopdf?

In a nutshell, it will render an entire web page (including images and JavaScript) to a PDF document.

Comments

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.