【发布时间】:2016-05-14 06:22:35
【问题描述】:
我正在研究一种解决方案,其中 Web 服务器(带有 express 的节点)将使用请求包从 Web api 获取数据。 数据返回(如果包含验证错误状态码),将再次匹配该值并返回相应的错误信息。怎么可能实现?
应该是这样的:
var options = {
method: 'POST',
url: 'apiUrl'
}
var response = function (error, response, body) {
if(!error && response.statusCode == 200){
res.jsonp(body);
} else {
if (body.language == 'en') {
// map the reponse body error status code to en.json
} else if (body.language == 'jp')
// map the response body error status code to jp.json
}
}
request({
options, response
})
验证错误的默认正文响应
{
'language': 'en',
'error': [{ 'ErrorCode': '1000', 'ErrorCode': '1001'}]
}
最终的身体反应(处理后)
{
'language': 'en',
'error': [{'ErrorMessage': 'Invalid data format', 'ErrorMessage': 'Invalid Password'}]
}
不同验证语言的资源文件(服务器中的静态)
en.json
{
'1000': 'Invalid date format',
'1001': 'Invalid password',
'1002': ...
'1003': ...
...
'1999': ...
}
jp.json
{
'1000': 'japan translation',
'1001': 'japan translation 2',
'1002': ...
'1003': ...
...
'1999': ...
}
【问题讨论】:
-
您的响应不是有效的 JSON,
ErrorCode键重复,您的意思是[{ 'ErrorCode': '1000' }, {'ErrorCode': '1001'}]?