简体   繁体   中英

How to display the console content in web browser?

I have example1.js nodejs file which is running fine in console

How can we do it to show in web browser using http request?

var request = require('request');
var cheerio = require('cheerio');

request('news.ycombinator.com', function (error, response, html) {
  if (!error && response.statusCode == 200) {
    var $ = cheerio.load(html);
    $('span.comhead').each(function(i, element){
      var a = $(this).prev();
      console.log(a.text());
    });
  }
});

How can we do it to show in web browser using http request?

You can't.

The request module is for making requests. You can't use it to listen for them.


You could do it with built-in http module . You could find an example of using it on the Node.js homepage .

But it's best to pick one of the HTTP server libraries available in npm.

Or to use one of node.js web application frameworks:

Then replace all your console.log statements with calls that put the text in the HTTP response bodies (as per the documentation for whichever module you pick).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM