【发布时间】:2021-01-04 09:31:15
【问题描述】:
我一直在使用带有简单 db.json 的 json-server 来模拟我的 REST API。
但是,我现在需要在后端的每个 POST 上保存当前日期。
我希望 json-server 在每个 POST 上生成一个时间戳并将其保存在 db.json 中,因此每次我执行 GET 请求时,它都会以记录保存的日期进行响应。
例如从这里开始:
{
"posts": [
{
"id": 1,
"title": "json-server",
"author": "typicode"
}
],
"comments": [
{
"id": 1,
"body": "some comment",
"postId": 1
}
]
}
到这里:
{
"posts": [
{
"id": 1,
"title": "json-server",
"author": "typicode"
}
],
"comments": [
{
"id": 1,
"body": "some comment",
"postId": 1,
"timeSaved": "2020-09-17T09:15:27+00:00"
}
]
}
【问题讨论】:
-
嗨,这可以通过@rnmkeshav/mock-api 包轻松实现。它允许您用 javascript 编写所有内容,并对请求和响应进行精细控制。如果您遇到任何问题,请在 package 的 github repo 上提出问题。
标签: javascript json rest json-server