配置
db.json
// 服务器数据库{ // 访问http:localhost:3000/profile "profile": [ { "id": 5, "username": "nana", "account": "209074080", "password": "123456" }, { "username": "18", "account": "19", "password": "22", "id": 1 }, { "username": "18", "account": "19", "password": "20", "id": 6 } ]}
json-server.json
// 服务器配置
{
// 开启的端口
"port":8080,
// 监听文件变化
"watch":true,
// 静态文件托管
"static":"./public"
}
配合axios
post 增
axios({
method:"post",
// 需要知道 path,且path存在
url:"profile",
baseURL:"http:localhost:3000"
data:{
username:"nana",
account:"315225",
password:"nana520"
// 可以携带id或者不携带,携带的id不能与数据库里的id重复
}
})
// value.data 为发送的data数据对象
.then((value)=>{value.data})
.catch((reason)=>{reason})
{
// 访问http:localhost:3000/profile
"profile": [
{
"id": 5,
"username": "kawaii",
"account": "1859978",
"password": "123456abc"
},
{
"id": 1,
"username": "kawaii",
"account": "1859978",
"password": "123456abc"
},
{
"username":"nana",
"account":"315225",
"password":"nana520"
// 不携带id,id 为最大id + 1
"id":6
}
]
}
delete 删
axios({
method:"delete",
// 需要知道 path 与 id
url:"profile/1",
baseURL:"http:localhost:3000"
})
// value.data 为空对象
.then((value)=>{value.data})
.catch((reason)=>{reason})
{
// 访问http:localhost:3000/profile
"profile": [
{
"id": 5,
"username": "kawaii",
"account": "1859978",
"password": "123456abc"
},
{
"username":"nana",
"account":"315225",
"password":"nana520"
"id":6
}
]
}
put 改
axios({
method:"post",
// 需要知道 path 与 id
url:"profile/5",
baseURL:"http:localhost:3000"
data:{
username:"anna",
account:"785418",
password:"anna520"
}
})
// value.data 为修改的data数据对象
.then((value)=>{value.data})
.catch((reason)=>{reason})
{
// 访问http:localhost:3000/profile
"profile": [
{
"id": 5,
"username": "anna",
"account": "785418",
"password": "anna520"
},
{
"username":"nana",
"account":"315225",
"password":"nana520"
"id":6
}
]
}
get 查
axios({
method:"get",
// 需要知道 path 与 id
// 访问 profile?username=anna&password=anna520,如果多个都满足则都返回
url:"profile/5",
baseURL:"http:localhost:3000"
})
// value.data 为返回的数据数组
.then((value)=>{value.data})
.catch((reason)=>{reason})
-------------------------------------
{
"id": 5,
"username": "anna",
"account": "785418",
"password": "anna520"
},