我有一个脚本用于从reddit中删除称为scraper.js的数据。它看起来像这样:
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');
});
});
这正是我希望它在运行时所做的事情:
node script.js
在我的终端中,但是如何在我的index.html文件中实现这一点?
答案 0 :(得分:0)
您可以将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>