How to view data from a Node.js script in an HTML page

Asked

Viewed 465 times

1

I wonder how I can run a Node.js script on an HTML page. The script is a Crawler, I mean, he pulls data from a page and I’d like to display the information he brings. I would also like to know if there is a similar function for HTML. Thank you!

  • Question: why does it have to run on the server? It cannot run on HTML itself?

  • Hello Gypsy. So it has to run on the server because of Node.js, I’m trying to find alternatives to run straight into html, but I can’t find.

  • Actually you need to match the response of @Sergio with some way for HTML to call a method on the server, like using a endpoint of Express.js.

  • Thanks @Ciganomorrisonmendez, I’ll try.

1 answer

2

There are some crawlers on NPM/Github, one of them the simplecrawler that I use.

A script I use is:

var domain = 'http://teu.dominio.com/';
var Crawler = require("simplecrawler");
var fs = require('node-fs');
var url = require('url');
var path = require("path");

new Crawler(domain).on("fetchcomplete", function(queueItem, responseBuffer, response) {

  var parsed = url.parse(queueItem.url);
  if (parsed.pathname === "/") {
    parsed.pathname = "/index.html";
  }

  // Diretoria de destino
  var outputDirectory = path.join(__dirname, 'tua_pasta');

  var dirname = outputDirectory + parsed.pathname.replace(/\/[^\/]+$/, "");
  var filepath = outputDirectory + parsed.pathname;
  fs.exists(dirname, function(exists) {
    if (exists) {
      fs.writeFile(filepath, responseBuffer, 'utf8', function() {});
    } else {
      fs.mkdir(dirname, 0755, true, function() {
        fs.writeFile(filepath, responseBuffer, function() {});
      });
    }

  });

}).start();

You need to configure the domain, and the destination folder, otherwise just run the file.

  • Hello Sergio. Thank you for the reply, have you any way I can run this direct script in html? Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.