我一直在使用带有简单db.json的json-server来模拟我的REST API。
但是,我现在要求在后端保存每个帖子的当前日期。
我希望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"
}
]
}发布于 2021-05-20 16:35:01
您可以将json-server作为一个模块与Express中间件:https://github.com/typicode/json-server#module结合使用。
他们有一个为POST请求保存createdAt的示例:
server.use((req, res, next) => {
if (req.method === 'POST') {
req.body.createdAt = Date.now()
}
// Continue to JSON Server router
next()
})或者您可以使用我的API服务器(基于json- server ):https://github.com/robinhuy/fake-rest-api-nodejs
https://stackoverflow.com/questions/63938876
复制相似问题