【发布时间】:2020-10-28 11:06:02
【问题描述】:
我将使用以下代码作为示例来构建我的问题。它基本上只是从 SQLite3 数据库中提取待办事项列表所需的代码:
那么,前端有一个axios请求:
useEffect(() => {
axios.get('http://localhost:3001/todo', {})
.then(res => {
setTodoList(res.data)
})
.catch(err => {
console.log(err)
})
}, [])
...在后端链接到以下函数:
server.get('/todo', (req,res) => {
// res.json(testData)
const todos = db('todos') //this is shorthand for 'return everything from the table 'todos''
.then(todos => {
return res.json(todos)
})
})
..来自这个 GET 请求的数据然后在反应组件中呈现,作为文本列表。
我只是对数据流感到困惑 - 什么时候是 HTTP,什么时候是 JSON,数据从数据库中以什么形式出现,以及这些不同的协议/语言如何相互通信其他的?
我了解了 GET 请求和异步函数的总体原理,但我只是不明白幕后发生了什么。谢谢!
【问题讨论】:
-
http 是一种协议。 json是一种数据格式。你似乎把他们弄糊涂了