如何向浏览器发送响应

时间:2020-10-24 21:34:06

标签: api http url response

  1. 我在这里所做的是通过Web截取instagram的标签URL。
  2. 我在终端VSCode上收到的是数组形式的数据,很棒。
  3. 我想做的是向浏览器路由(app.get/data)发送响应,该响应将显示我在终端上看到的所有这些数据。
const rp = require('request-promise');
const cheerio = require('cheerio');
const express = require('express')
const app = express()
const port = 3000

app.get('/data', (req, res, next) => {
    //res.??????
})
app.listen(port, () => {
    console.log(`Example app listening at http://localhost:${port}`)
})

let keyWord = "hair"
let URL = `https://www.instagram.com/explore/tags/${keyWord}/`

rp(URL)
    .then((html) => {
        let hashtags = scrapeHashtags(html);
        hashtags = removeDuplicates(hashtags);
        hashtags = hashtags.map(ele => "#" + ele)
        console.log(hashtags);
    })
    .catch((err) => {
        console.log(err);
    });

const scrapeHashtags = (html) => {
    var regex = /(?:^|\s)(?:#)([a-zA-Z\d]+)/gm;
    var matches = [];
    var match;

    while ((match = regex.exec(html))) {
        matches.push(match[1]);
    }
    return matches;
}

const removeDuplicates = (arr) => {
    let newArr = [];
    arr.map(ele => {
        if (newArr.indexOf(ele) == -1) {
            newArr.push(ele)
        }
    })
    return newArr;
}

0 个答案:

没有答案
相关问题