There is a code snippet in api/scraper.js file that I need help with.
const request = require("request-promise");
const cheerio = require("cheerio");
let url = "https://crese.org/distintivo-azul/";
let result;
request(url, (err, response, html) => {
if (!err && response.statusCode == 200) {
const $ = cheerio.load(html);
const allText = $("html *").contents().filter(function () {return this.type === "text"}).text();
const texts = ["respeto a la dignidad de las personas", "respect for the dignity of people"]
result = allText.includes("respeto a la dignidad de las personas")
}
})
export default function handler(req, res) {
res.status(200).json({result: result})
}
The code is functioning properly, but I need to dynamically change the url variable. I have a form on the client side where users can input the new URL... How can I update the url variable on the server side when the user submits the form?
In simple terms, I want to "Modify the url variable in api/scraper.js using the form input from the client side."
Any suggestions on how to achieve this?
EDIT Here's what the client side looks like:
// Automate steps process
const handleSubmit = async (event) => {
event.preventDefault();
const response = await fetch("/api/scraper");
if (!response.ok) {
throw new Error(`Error: ${response.status}`);
}
const result = await response.json();
console.log(Object.values(result))
return setData(result)
//console.log(event.target.url.value);
};
// get results from backend
const [results, setData] = useState([])
return (
<div>
<Head>
<title>Blue Token | Steps</title>
<meta name="description" content="Generated by create next app" />
<link rel="icon" href="/logo.png" />
</Head>
<div className={styles.minting}>
<form onSubmit={handleSubmit}>
<h1>Enter Your website URL</h1>
<label>We are going to check if you have the requesits</label><br /><br />
<input type="url" name="url" placeholder="url" />
<input type="submit" />
</form>