我是RESTful API的新手,并且我已成功为我的API实现了GET和DELETE命令(GET localhost:4000 / api,Postman上的DELETE localhost:4000 / api工作正常)。当我尝试发布我的API时,我希望它能给我一种格式
{
"message": "OK",
"data": [
{
result goes here
}
}
但是当我发布它时,它只给我结果本身
{
"__v": 0,
"name": "clean room",
"description": "to clean my room",
"completed": false,
"deadline": "1970-01-01T00:00:02.017Z",
"_id": "59fb9522a3617147904a20ed",
"dateCreated": "2017-11-02T21:58:58.163Z",
"assignedUserName": "unassigned",
"assignedUser": ""
}
我的POST代码如下:
router.post('/', function(req, res){
var newTask = new task(req.body);
newTask.save(function(err, tasks){
if(err) {
res.status(404).send({
message: err,
data: []
});
} else {
res.status(201).send({
message: 'OK',
data: res.json(tasks)
});
}
})
});
它正确发布了API,但我想提供正确的格式,如上所述。