如何让网站在浏览器中自动运行"node index.js"?

How can I make the website automatically run "node index.js" in the browser?

我有一个 javascript 的股票价格网络抓取程序,我想在 netlify 上自动 运行。

为了 运行 我必须在命令终端中输入“node index.js”。

但是..我怎样才能让它在上传到 netlify 后自动执行 - 而无需手动输入“node index.js”? Web Scraping/API Practice

这是我的第一个网络抓取项目 - 所以请谅解 - 这可能是一个 VERRY 菜鸟问题。

这是 index.js 文件

const request = require("request-promise");
const fs = require("fs");
const cheerio = require("cheerio");


async function tsla() {
    const html = await request.get(
        "https://finance.yahoo.com/quote/TSLA/"
        );

    const $ = await cheerio.load(html);

    const Price = $('[class="Fw(b) Fz(36px) Mb(-4px) D(ib)"]').text();
    const Symbol = $('[class="D(ib) Fz(18px)"]').text();
    const PercentChange = $('fin-streamer[class="Fw(500) Pstart(8px) Fz(24px)"][data-field="regularMarketChangePercent"]').find('span').text();
    fs.writeFileSync("./tslaSymbol.csv", Symbol);
    fs.writeFileSync("./tslaPrice.csv", Price);
    fs.writeFileSync("./tslaPercentChange.csv", PercentChange)
    
    console.log(Price);
    console.log(Symbol);
    console.log(PercentChange)

    setTimeout(tsla, 1000); //60 seconds == 1minute
}

tsla();

async function aapl() {
    const html = await request.get(
        "https://finance.yahoo.com/quote/AAPL/"
        );

    const $ = await cheerio.load(html);

    const Price = $('[class="Fw(b) Fz(36px) Mb(-4px) D(ib)"]').text();
    const Symbol = $('[class="D(ib) Fz(18px)"]').text();
    const PercentChange = $('fin-streamer[class="Fw(500) Pstart(8px) Fz(24px)"][data-field="regularMarketChangePercent"]').find('span').text();
    fs.writeFileSync("./aaplSymbol.csv", Symbol);
    fs.writeFileSync("./aaplPrice.csv", Price);
    fs.writeFileSync("./aaplPercentChange.csv", PercentChange)
    
    
    console.log(Price);
    console.log(Symbol);

    setTimeout(aapl, 1000); // 60,000 = 60 seconds == 1minute
}

aapl();


async function spy() {
    const html = await request.get(
        "https://finance.yahoo.com/quote/spy/"
        );

    const $ = await cheerio.load(html);

    const Price = $('[class="Fw(b) Fz(36px) Mb(-4px) D(ib)"]').text();
    const Symbol = $('[class="D(ib) Fz(18px)"]').text();
    const PercentChange = $('fin-streamer[class="Fw(500) Pstart(8px) Fz(24px)"][data-field="regularMarketChangePercent"]').find('span').text();
    fs.writeFileSync("./spySymbol.csv", Symbol);
    fs.writeFileSync("./spyPrice.csv", Price);
    fs.writeFileSync("./spyPercentChange.csv", PercentChange)
    
    console.log(Price);
    console.log(Symbol);
    console.log(PercentChange);

    setTimeout(spy, 1000); // 60,000 = 60 seconds == 1minute
}

spy();

async function nvda() {
    const html = await request.get(
        "https://finance.yahoo.com/quote/nvda/"
        );

    const $ = await cheerio.load(html);

    const Price = $('[class="Fw(b) Fz(36px) Mb(-4px) D(ib)"]').text();
    const Symbol = $('[class="D(ib) Fz(18px)"]').text();
    const PercentChange = $('fin-streamer[class="Fw(500) Pstart(8px) Fz(24px)"][data-field="regularMarketChangePercent"]').find('span').text();
    fs.writeFileSync("./nvdaSymbol.csv", Symbol);
    fs.writeFileSync("./nvdaPrice.csv", Price);
    fs.writeFileSync("./nvdaPercentChange.csv", PercentChange)
    
    console.log(Price);
    console.log(Symbol);
    console.log(PercentChange);

    setTimeout(nvda, 1000); // 60,000 = 60 seconds == 1minute
}

nvda();

基本上,javascript 抓取数据并将其发送到 CSV 文件,然后显示在页面中。

我认为在将项目部署到 netlify 之前,您必须在项目中创建两个文件,

  1. netlify.toml(您将放置构建函数的位置)
  2. 您必须创建一个包含空 index.html 文件的 dist 目录。

如果你知道,就忽略它,然后 确保你已经在 package.json 中编写了脚本,然后在部署时你应该能够配置你的构建命令,它应该没问题。

你也可以从这里得到一些帮助netifly functions are the ways to do since netlify hosts static compatible