正常返回的数据:
{“message”:“success”,“count”:22,“status”:200,“data”:[{“useCommonCoupon”:56,“dateKey”:“2015-12-01”},{“useCommonCoupon”:59,“dateKey”:“2015-12-02”},{“useCommonCoupon”:59,“dateKey”:“2015-12-03”},{“useCommonCoupon”:62,“dateKey”:“2015-12-04”}]}
但是代码放到服务器端,每次请求返回的数据都是错误的:
{“message”:“success”,“count”:22,“status”:200,“data”:[{“useCommonCoupon”:56,“dateKey”:“2015-12-01”},{“useCommonCoupon”:59,"dateK
后面的数据丢失,导致前台无法解析。
代码:
async function getData(){
let sql="SELECT * FROM int_information "
let rows=await query(sql);
return rows;
}
getData().then(data=>res.send({flag:1,data:data})).catch(error=>{
res.send({flag:0,err:error});//flag 0为错误标识码
});
找了很久,原因在于node 8.x出现的keepalivetimeout属性,默认只有5s,所以数据量大的时候,数据还没传输完毕,node就已经关闭了。
官方API:
server.keepAliveTimeout#
Added in: v8.0.0
<number> Timeout in milliseconds. Defaults to 5000 (5 seconds).
The number of milliseconds of inactivity a server needs to wait for additional incoming data, after it has finished writing the last response, before a socket will be destroyed. If the server receives new data before the keep-alive timeout has fired, it will reset the regular inactivity timeout, i.e., server.timeout.
A value of 0 will disable the keep-alive timeout behavior on incoming connections.
Note: The socket timeout logic is set up on connection, so changing this value only affects new connections to the server, not any existing connections.
解决方案:
在app.js中加入:
server.keepAliveTimeout=30000;//根据项目实际情况,设置时间