【发布时间】:2021-10-09 23:02:50
【问题描述】:
这是我的后端路由,我从 URL 获取文件名作为参数并访问该文件。如何访问从这条路由传递到我的前端 react.js 的数据??
router.route("/list/:filename").get((req, res) => {
fs.readFile("./api/assignment_data/" + req.params.filename + ".json", function read(err, data) {
if (err) {
throw err;
}
const content = data;
foundFile => res.json(foundFile)
console.log("sent")
})
})
在我的前端我正在做这样的事情,
useEffect(() => {
fetch("/list/:filename").then(res => {
if (res.ok) {
console.log("all ok")
return res.json()
}
}).then(jsonRes => setMetrics(jsonRes))
})
【问题讨论】:
标签: node.js reactjs express mern express-router