正如我在上面评论的那样,要么使用 myjson,要么你可以使用 json-server 包。
在您的项目文件夹中,您只需要创建一个 json 文件 ex. local-api.json
例如:
{
"posts": [
{ "id": 1, "title": "json-server", "author": "typicode" }
],
"comments": [
{ "id": 1, "body": "some comment", "postId": 1 }
],
"profile": { "name": "typicode" }
}
你可以这样运行它:
json-server --watch local-api.json
注意:示例取自那里以便您理解。你可以在他们的 GitHub 网站上探索更多关于这个包的信息
更多参考请访问 GitHub 站点:here
Express 服务器示例
const express = require('express')
const app = express()
const port = 5000
app.get('/', (req, res) => res.json({"hello":"world"})) //add your data
app.listen(port, () => console.log(`Example app listening on port ${port}!`))
使用 node index.js 运行这个文件并确保你安装了 express(
npm install express --save)
然后你可以像这样获取你的组件:
componentDidMount(){
axios.get("http://localhost:5000").then(data => //your logic)
}