提问人:Bernardo Olisan 提问时间:4/30/2022 最后编辑:Bernardo Olisan 更新时间:4/30/2022 访问量:1152
如何在客户端 nextjs 中更改服务器端变量
How to change Server side variables in the client side nextjs
问:
所以我在 api/scraper.js 中有这段代码
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})
}
它都可以工作,但是let url变量需要更改。我有一个表单来处理此更改,因此用户可以从客户端将 url 放入输入表单中......当用户从客户端发送 URL“输入表单”并更改让 URL 在服务器端 var iable 时,如何更改它。
简单来说,“修改 api/scraper.js 中的 url varible”,带有客户端的表单标记。
有什么想法可以处理这个问题吗?
编辑客户端如下所示
// 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>
答:
1赞
Inder
4/30/2022
#1
你可以做这样的事情。您必须在 http 请求的正文中从客户端传入 url
function startScrape(req, res) {
request(req.body, (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"
];
let result = allText.includes("respeto a la dignidad de las personas");
res.status(200).json({ result: result });
}
});
}
export default function handler(req, res) {
if(req.method==='POST')
startScrape(req, res);
}
客户
const [results, setData] = useState([]);
const [url, setUrl] = useState("");
const handleSubmit = async (event) => {
event.preventDefault();
// Here we're passing url inside body of the request which will
// be received on the backend
const response = await fetch("/api/scraper", { method: "POST", body: url });
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);
};
return (
<div>
<div>
<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"
value={url}
onChange={(e) => setUrl(e.target.value)}
/>
<input type="submit" />
</form>
</div>
</div>
);
评论
0赞
Inder
4/30/2022
此外,请求包已弃用,您应该使用任何其他包,例如 axios
0赞
Bernardo Olisan
4/30/2022
req.body.url 这是如何工作的?你能给我一个例子,在客户端中有一个访问这个 req.body.url 的小脚本吗?
0赞
Inder
4/30/2022
您在客户端输入标签的问题中提到了@bernardoolisan?
0赞
Bernardo Olisan
4/30/2022
是的,我有一个,请编辑我的问题,以便您可以了解我的客户的工作方式
0赞
Bernardo Olisan
4/30/2022
完成,编辑就在那里
评论