res.json supports objects, just write res.json({data, message: 'success'}) like this. If you use strings, it is best to use res.send, res.end, otherwise res.json has to determine the type.
The object returned in this way is
{
message: 'success',
data: {} // 原有data.
}
It is recommended to do this, with a layer of packaging. If you add the data field directly to message, if data itself also has a message field, wouldn’t it be overwritten?
The easiest way:
But it will be annoying to write like this all the time. You will gradually optimize your writing
res.json
supports objects, just writeres.json({data, message: 'success'})
like this. If you use strings, it is best to useres.send
,res.end
, otherwiseres.json
has to determine the type.The object returned in this way is
It is recommended to do this, with a layer of packaging. If you add the
data
field directly tomessage
, ifdata
itself also has amessage
field, wouldn’t it be overwritten?