有時,我們需要在將響應JSON數據發送到客戶端之前進行修改。例如:什麼是格式化/定製遠程響應的最佳方式?
//model definition
{
"name": "File",
"base": "PersistedModel",
"properties": {
"filename": {
"type": "string",
"required": true
},
"filepath": {
"type": "string"
}
}
"protected": ["filepath"]
}
我想獲得GET /files/:id
請求url
屬性,所以我定義原型的URL吸氣劑。
//file.js
module.exports = function(File){
var baseUrl = 'http://example.com/uploads/files/';
File.prototype.__defineGetter__('url', function(){
return baseUrl + this.id.toString() + this.filename;
});
}
我的問題是如何暴露url
屬性遠程響應時,我提出一個要求如下?
GET /files/123456
期待類似這樣的回覆:
{
id: '123456',
filename: 'myfile.ext',
url: 'http://example.com/uploads/files/123456/myfile.ext'
}
非常感謝!
這是一個真正的壞問題? – XXLIVE