Gra*_*ams 0 javascript node.js
我想通过nodejs获取一些html并在浏览器中获取输出,所以我使用了以下代码
const express = require('express')
const app = express()
const fetch = require("node-fetch");
const port = 3000
app.listen(port, () => console.log(`Example app listening on port ${port}!`));
fetch("https://example.com/results.php") .then(res => res.text())
.then(data => obj = data)
.then(() =>
app.get('/', (req, res) => res.send(obj))
)
Run Code Online (Sandbox Code Playgroud)
然后我开始使用应用程序 node app
现在,当我运行时localhost:3000,每次都会提供相同的输出,但https://example.com/results.php是动态结果页面,它会在每次重新加载时返回各种结果.
所以我需要的是每次我打开localhost:3000,它必须再次获取url并在浏览器窗口中返回新结果,抱歉我对nodejs是全新的,我只是想从nodejs重新制作php curl.
您需要将获取登录信息放在GET路由中.
从逻辑上考虑一下.你想要做的是:
当用户请求/页面时,请获取" https://example.com/results.php "并将结果发送给用户.
因此,/路线必须始终可用,当它被击中时,您将获取所需的资源.以下是它的翻译方式:
const express = require('express');
const app = express();
const fetch = require("node-fetch");
const port = 3000;
app.get("/", (req, res) => {
fetch("https://example.com/results.php")
.then(res => res.text())
.then((obj) => {
res.send(obj);
})
})
app.listen(port, () => console.log(`Example app listening on port ${port}!`));
Run Code Online (Sandbox Code Playgroud)