0

We have a requirement to load the input HTML string into browser kind of environment, run the html (which should internally run all the inline scripts i.e there are a few ajax calls) and then provide the rendered HTML as output.

Is this possible with node?

Is there any node module which we can use for this purpose.

Please help in this regard.

7
  • angularJS is, in my opinion, one of the best Node modules for web page rendering Commented Dec 14, 2016 at 10:00
  • Thanks for your opinion. Current problem that we are trying to address is that; we should be able to load html string in browser in the background which should make all the ajax calls internally to get data and few scripts would be run to update HTML DOM based on the data retrieved. Thereby, entire HTML would be rendered. Then we should be able to get the entire rendered HTML content as a string. Please help. Commented Dec 14, 2016 at 10:05
  • wouldn't it be better to create templates that you add to the requested page if needed? fetching a dozen diferent HTML elements with Ajax and getting the content itself will get really taxing on the server if you have lots of users at once using your website, it'd be better to fetch the data you need, and then send it over to the client, rendering it there, instead of rendering it on the server and then sending it to the user Commented Dec 14, 2016 at 10:10
  • There would not be any server side hits. There would be a tool which would be making a call to get the rendered HTML. Hence, the issue with multiple users would not exist. Commented Dec 14, 2016 at 10:19
  • NodeJS is a server sided thing, it's your server Commented Dec 14, 2016 at 10:21

1 Answer 1

1

Sound like you looking for headless browser for NodeJS. see a list here http://github.com/dhamaniasad/HeadlessBrowsers

This is example from CasperJS

You can install as Node module

npm install -g casperjs

And

var casper = require('casper').create();

casper.start('http://www.google.fr/', function() {
    this.echo(this.getHTML());
});

casper.run();

Run

casperjs app.js

For NODEJS runtime

Try Nightmare

Install

npm install nightmare

code

var Nightmare = require('nightmare');
var nightmare = Nightmare({ show: false });

nightmare.goto("http://www.google.com")
.evaluate(function(){
    return document.body.outerHTML;
})
.end()
.then(function (result) {
        console.log(result)
 })

Run

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

3 Comments

Thanks. This line "var casper = require('casper').create();" shows error "Cannot find module casper". To confirm, we have installed the node module "casperjs" accordingly. Please help.
Below is the code that I have placed in app.js in node-app. var casper = casperjs.create(); casper.start('google.fr', function() { this.echo(this.getHTML()); }); casper.run(); Then while trying to run using node with command "node app.js", the error "Cannot find module casper" is seen.
@user3220129 casperjs need it's own runtime. for nodejs compatible try Nightmare. I editted the answer.

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.