【发布时间】:2021-12-18 22:48:21
【问题描述】:
我正在尝试将简单数据从我的服务器传递到另一个 html 页面上调用的 javascript 文件。我正在测试从服务器发送单个字符串,但不确定如何接收它。以下服务器:
const express = require('express')
const app = express()
const port = 3000
app.use(express.static("./assets"));
app.get('/', (req, res) => {
//res.send('Hello World!')
res.sendFile('./main.html', { root: __dirname });
})
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})
app.get('/get-test', async (_req, res) => {
try {
const test_string = "get-test request string";
return res.send(test_string);
} catch (e) { throw e; }
});
在另一个 javascript 文件中,我有以下内容:
async function testing() {
const response = await fetch('/get-test');
console.log(response);
}
testing();
console.log 给了我一个完整的对象,点击它我似乎在任何地方都找不到我的test_string:
所以我相信get 请求有效,但我如何真正访问我想要的内部数据?
【问题讨论】:
标签: javascript node.js express