简体   繁体   English

在index.html中使用node.js脚本

[英]Using node.js scripts in index.html

I have a script that is used to scrape data off of reddit called scraper.js. 我有一个脚本,用于从reddit抓取数据,称为scraper.js。 It looks like this: 看起来像这样:

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

request("https://www.reddit.com/r/all", function(error, response, body) {
if(error) {
    console.log("Error: " + error);
}
console.log("Status code: " + response.statusCode);

var $ = cheerio.load(body);

$('div#siteTable > div.link').each(function( index ) {
    var title = $(this).find('p.title > a.title').text().trim();
    var time = $(this).find('p.tagline').text().trim();
    var user = $(this).find('a.author').text().trim();


    console.log("Title: " + title);
    console.log("Author: " + user);
    console.log("Time: " + time);
    // possibly wrap this part in <li> tags???
    fs.appendFileSync('reddit.txt', title + '\n');

});
});

This does exactly what I want it to do when run: 这正是我想要在运行时执行的操作:

node script.js

in my terminal, but how do I go about getting this to happen in my index.html file? 在终端中,但是如何在index.html文件中进行此操作?

you can use Node Express for both client side and server side together. 您可以同时在客户端和服务器端使用Node Express。

 const express = require('express') const app = express() app.set('view engine', 'ejs'); app.get('/', (req, res) => res.render('index', { title: 'Express' }); app.listen(3000, () => console.log('Example app listening on port 3000!')) 
 file name use index.ejs <h1>test</h1> <div ><%= title %></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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