[英]Getting a CORS err and GCP functions err "Function invocation was interrupted. Error: memory limit exceeded" while trying to scrape a site using React
Im using React and I'm attempting to scrape a site from my frontend utilizing a Firebase cloud function and Puppeteer.我正在使用 React,我正在尝试使用 Firebase 云 function 和 Puppeteer 从我的前端抓取网站。 Oddly enough I get the code working on my local server, but when attempting to implement the same code on my live server via firebase, I'm getting this CORS error below.
奇怪的是,我的代码在我的本地服务器上运行,但是当尝试通过 firebase 在我的实时服务器上实现相同的代码时,我在下面收到了这个 CORS 错误。
Below is the code on my frontend, where I make the request:以下是我提出请求的前端代码:
import React, {useState} from "react";
import {Typography, Container, TextField, Button} from "@material-ui/core";
import axios from 'axios'
export const Scraper = ({ formData, setForm, navigation }) => {
const { firstName, lastName, displayName, services} = formData;
const [url, setUrl] = useState('')
const [html, setHtml] = useState('')
const handleScrape = async() => {
try {
const response = await axios.get('https://us-central1-cuti-app-7c963.cloudfunctions.net/app/scrape', {
params: {url: url}
})
if(response){
console.log(response.data)
}else{
console.log("Failure Link retrieval")
}
} catch (error) {
console.log("This is the Error:", error)
}
return (
<Container maxWidth="xs" style={{marginTop:'20px', textAlign: "center"}}>
<Typography variant='h4'>Prospect URLL</Typography>
<TextField
label="Booksy"
name="url"
value={url}
onChange={(e)=> {setUrl(e.target.value)}}
margin="normal"
variant="outlined"
autoComplete="off"
required
fullWidth
/>
<Button
variant="contained"
fullWidth
color="primary"
style={{ backgroundColor: '#cf559f',
backgroundSize: "400px",
backgroundPosition: "center",
borderRadius: '0',
color: 'white',
fontWeight: 'bold',
border: '3px #cf559f solid',
letterSpacing: '2px',
marginTop: "1rem" }}
onClick={handleScrape}
>
Next
</Button>
</Container>
);
};
And here is my code snippet from my index.js file form my functions:这是我的 index.js 文件中的代码片段,来自我的函数:
const express = require("express");
const app = express();
const cors = require("cors");
app.use(express.urlencoded({extends: true}));
app.use(express.json());
const corsOpts = {
origin: '*',
methods: [
'GET',
'POST',
],
allowedHeaders: [
'Content-Type', 'Authorization', 'Accept'
],
};
app.use(cors(corsOpts))
const functions = require("firebase-functions");
const admin = require('firebase-admin')
admin.initializeApp(functions.config().firebase)
const puppeteer = require('puppeteer');
app.get('/scrape', cors(), async(req, res) => {
let { url } = req.query
try{
// let services = []
const browser = await puppeteer.launch()
const page = await browser.newPage()
page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36');
await page.goto(url)
const proInfo = await page.evaluate(() => {
const displayName = document.querySelector('h1').innerText
const phone = document.querySelector('div[class="purify_dFcdTMoibUeU0IQhEe9mHA=="]').innerText
const imgUrl = document.querySelector('div[class="purify_W9xnvEHvIASJ3h0FC-rz7Q=="]').children[0].currentSrc
const address = document.querySelector('div[class="purify_prm7MfDXczhTZvcY5KwOuA== purify_Sardy6hfiet162IZ2pYFPA== purify_m9mNOPjpHD0tNTW6GC+hEw=="]').innerText
return({img: imgUrl, displayName: displayName, phone: phone, address: address})
}
)
const services = await page.$$eval('div[class="purify_TJBmvp84N9Sj6dyMFksHKg=="]', divs => {
return divs.map(x => {
return({
name: x.children[0].children[0].innerText,
details: x.children[0].children[1].innerText,
price: x.children[1].children[0].children[0].children[0].children[0].children[0].innerText,
duration: x.children[1].children[0].children[0].children[0].children[0].children[1].innerText
})
})
})
res.json({proInfo: proInfo, services: services})
}catch (e) {
console.log("ERROR =>", e)
}
})
Any and all assistance would be GREATLY appreciated!!!任何和所有的帮助将不胜感激!!!
UPDATE #1: Made a couple of edits to the CORS options... but still getting the same ERR with a 500 code..更新 #1:对 CORS 选项进行了一些编辑......但仍然得到相同的 ERR,代码为 500..
The img below is what I see on the Network tab下面的 img 是我在“网络”选项卡上看到的
UPDATE #2: Looking deeper into the err, I noticed the logs on GCP and it states "Function invocation was interrupted. Error: memory limit exceeded."更新#2:深入研究错误,我注意到 GCP 上的日志,它指出“函数调用被中断。错误:超出 memory 限制。”
See img below.见下图。
The NEW question is: Can we increase the memory limit?新问题是:我们可以增加 memory 限制吗? If so, how?
如果是这样,怎么做?
You must define allowed origin to permite your front-end and back end to interact.您必须定义允许的来源以允许您的前端和后端交互。 You use origin but they are define no where*
您使用原点,但它们没有定义在哪里*
Seems like you need to pass a couple of more options to cors.似乎您需要向 cors 传递更多选项。
Try this in your index.js
file.在你的
index.js
文件中试试这个。
const corsOpts = {
origin: '*',
methods: [
'GET',
'POST',
],
allowedHeaders: [
'Content-Type',
'Authorization',
'Accept',
],
};
app.use(cors(corsOpts));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.