import Review from '../models/Review.js' import cheerio from "cheerio"; import axios from 'axios'; const search = async (req, res, next) => { console.log(req.reviews, "1") try { for (let j = 1; j < 2; j++) { let reviews = [] // console.log(req.reviews, "제발")/ let url = "https://www.google.com/search?q=" + encodeURI(req.query.keyword) + "+site%3Atistory.com&page_no=" + j let response1 = await axios.get(url) let $1 = cheerio.load(response1.data); $1('.kCrYT').each(async function (i) { console.log("3") let title = $1(this).find('h3').text() let searchParams = new URLSearchParams($1(this).find('a').attr('href')); let link = searchParams.get("/url?q") let summary = $1(this).find('.s3v9rd').find('.s3v9rd').text() if (title) { reviews[i] = { title: title, link: link } } else if (summary) { reviews[i - 1] = { ...reviews[i - 1], summary: summary } reviews = reviews.filter(e => e) } }) reviews.forEach((review, i) => { Review.findOne({ link: review.link }, async function (err, reviewss) { console.log("4") if (err) { return res.status(404).send({ error: err }) } if (reviewss) { reviewss.keyword = [...reviewss.keyword, req.query.keyword] reviewss.save() } if (!reviewss) { reviews["keyword"] = [req.query.keyword] let Reviews = await new Review(review).save() // console.log(Reviews,"1") req.reviews.push(Reviews) } }) }) } const a = await Promise.all(req.reviews) console.log(a, "2") res.status(200).send({ review: a, db: true }) } catch (error) { console.log("6") console.log(error) } } const find = (req, res, next) => { console.log("7", req.query.keyword) const keyword = req.query.keyword if (req.body.db) { next() } Review.find({ keyword: keyword }, function (err, reviews) { req.reviews = [] if (reviews) { if (reviews.length > 10) { res.status(200).send({ review: reviews, db: true }) } req.reviews = reviews } next() }) } export default { search, find }