码迷,mamicode.com
首页 > Windows程序 > 详细

res.api用法说明

时间:2015-06-18 11:47:38      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:

# res.api res.api is an express middleware for render json api , it convention over api format like this : { data: { }, status: { code : x, msg : ‘some message‘ } } more see at [cnodejs 客户端 API 开发总结](https://cnodejs.org/topic/552b3b9382388cec50cf6d95) ## Install npm install --save res.api ## Usages var express = require(‘express‘); var res.api = require(‘res.api‘); var app = new express(); app.use(res.api); then in some route ### way 1 return res.api(404 ,err, { code : 1, msg : ‘delete failed!‘ }); the response header is : HTTP/1.1 404 Not Found X-Powered-By: Express Access-Control-Allow-Origin: * Access-Control-Allow-Methods: GET, POST Access-Control-Allow-Headers: X-Requested-With,content-type, Authorization Content-Type: application/json; charset=utf-8 Content-Length: 239 ETag: W/"ef-6e66e2da" set-cookie: connect.sid=s%3ApwL-xMS2tCh3qgqp_wyIqukbUKFeJv6S.2EB4449yTlxRWZrRyBXRc9J6Pv%2BNz4M7j35VLIlxE6M; Path=/; Expires=Wed, 17 Jun 2015 15:11:28 GMT; HttpOnly Date: Wed, 17 Jun 2015 14:41:28 GMT Connection: keep-alive response json data { "data": { "message": "Cast to ObjectId failed for value \"557a3e326221681d474cf078sdsds\" at path \"_id\"", "name": "CastError", "kind": "ObjectId", "value": "557a3e326221681d474cf078sdsds", "path": "_id" }, "status": { "code": 1, "msg": "delete failed!" } } ### way 2 return res.api(data, { code : 1, msg : ‘delete failed!‘ }); the response header is : HTTP/1.1 200 Ok X-Powered-By: Express Access-Control-Allow-Origin: * Access-Control-Allow-Methods: GET, POST Access-Control-Allow-Headers: X-Requested-With,content-type, Authorization Content-Type: application/json; charset=utf-8 Content-Length: 239 ETag: W/"ef-6e66e2da" set-cookie: connect.sid=s%3ApwL-xMS2tCh3qgqp_wyIqukbUKFeJv6S.2EB4449yTlxRWZrRyBXRc9J6Pv%2BNz4M7j35VLIlxE6M; Path=/; Expires=Wed, 17 Jun 2015 15:11:28 GMT; HttpOnly Date: Wed, 17 Jun 2015 14:41:28 GMT Connection: keep-alive response json data { "data": { "message": "Cast to ObjectId failed for value \"557a3e326221681d474cf078sdsds\" at path \"_id\"", "name": "CastError", "kind": "ObjectId", "value": "557a3e326221681d474cf078sdsds", "path": "_id" }, "status": { "code": 1, "msg": "delete failed!" } } ### way 3 return res.api(data); the response header is : HTTP/1.1 200 Ok X-Powered-By: Express Access-Control-Allow-Origin: * Access-Control-Allow-Methods: GET, POST Access-Control-Allow-Headers: X-Requested-With,content-type, Authorization Content-Type: application/json; charset=utf-8 Content-Length: 239 ETag: W/"ef-6e66e2da" set-cookie: connect.sid=s%3ApwL-xMS2tCh3qgqp_wyIqukbUKFeJv6S.2EB4449yTlxRWZrRyBXRc9J6Pv%2BNz4M7j35VLIlxE6M; Path=/; Expires=Wed, 17 Jun 2015 15:11:28 GMT; HttpOnly Date: Wed, 17 Jun 2015 14:41:28 GMT Connection: keep-alive response json data { "data": { "message": "Cast to ObjectId failed for value \"557a3e326221681d474cf078sdsds\" at path \"_id\"", "name": "CastError", "kind": "ObjectId", "value": "557a3e326221681d474cf078sdsds", "path": "_id" }, "status": { "code": 0, "msg": "deleterequest success!" } } 一直想写,今天终于写出来了,懒人还是有救的

res.api用法说明

标签:

原文地址:http://my.oschina.net/nodeonly/blog/468222

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!