res.api用法说明

# 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!"
}
}

一直想写,今天终于写出来了,懒人还是有救的

时间: 2024-10-08 11:58:22

res.api用法说明的相关文章

HTML5 Canvas八大核心技术及其API用法

什么是canvas? Canvas元素是HTML5的一部分,允许脚本语言动态渲染 位图像.Canvas由一个可绘制区域HTML代码中的属性定义高度和宽度(注:用其属性width和height设置宽度和高度时不能跟像素单位 “px”).JavaScript代码可访问该区域,通过一套完整的绘图功能类似于其他通用二维的API,从而生成动态的图形. Canvas八大核心技术(3D3R公司创始人兼CEO Ohad Eder-Pressman的独到见解): 1.游戏 HTML5在基于Web的图像显示方面比F

Java操作mongoDB2.6的常见API用法

对于mongoDB而言,学习方式和学习关系型数据库差不太多 开始都是学习如何insert.find.update.remove,然后就是分页.排序.索引,再接着就是主从复制.副本集.分片等等 最后就是通过它提供的各个驱动(比如Java.PHP.node.js等等)来练习所谓的高级用法 另外:相对于Oracle有PLSQL Developer,MySQL有HeidiSQL mongoDB也不例外,它的图形化工具中有一款叫做mongoVUE的,我用的是1.5.3破解版(不是每15天就得改一次注册表的

JavaEE基础(02):Servlet核心API用法详解

摘自:https://www.cnblogs.com/cicada-smile/p/12020195.html 本文源码:GitHub·点这里 || GitEE·点这里 一.核心API简介 1.Servlet执行流程 Servlet是JavaWeb的三大组件之一(Servlet.Filter.Listener),它属于动态资源.Servlet的作用是处理请求,服务器会把接收到的请求交给Servlet来处理,在Servlet中通常需要:接收请求数据:处理请求:完成响应. 2.核心API简介 API

Android Preference API 用法--ListPreference(一)

一.ListPreference简介 我们都只知道SharedPreference非常适合于参数设置功能,在此处的preference 也是代表SharedPreference的意思,在SharedPreference中,我们可以迅速的将某些值保存进xml文件中,然后我们可以读取这些设置信息进行相应的操作. 为了简化与preference相关的应用开发,android为我们提供了一系列的api来帮助我们.主要有 PreferenceActivity,CheckBoxPreference,Edit

Elasticsearch批量操作API用法介绍

Elasticsearch的Bulk API允许批量提交index和delete请求,有如下两种用法: 用法1 BulkRequestBuilder requestBuilder = client.prepareBulk(); for(Person person : personList){ String obj = getIndexDataFromHotspotData(person); if(obj != null){ requestBuilder.add(client.prepareInd

Java 8新特性stream API用法总结

前言 Stream 作为 Java 8 的一大亮点,它与 java.io 包里的 InputStream 和 OutputStream 是完全不同的概念.它也不同于 StAX 对 XML 解析的 Stream,也不是 Amazon Kinesis 对大数据实时处理的 Stream.Java 8 中的 Stream 是对集合(Collection)对象功能的增强,它专注于对集合对象进行各种非常便利.高效的聚合操作(aggregate operation),或者大批量数据操作 (bulk data

ServerSocketChannel API用法

java.nio.channels 类 ServerSocketChannel java.lang.Object java.nio.channels.spi.AbstractInterruptibleChannel java.nio.channels.SelectableChannel java.nio.channels.spi.AbstractSelectableChannel java.nio.channels.ServerSocketChannel 所有已实现的接口: Closeable,

SelectionKey API 用法

java.nio.channels 类 SelectionKey java.lang.Object java.nio.channels.SelectionKey 直接已知子类: AbstractSelectionKey public abstract class SelectionKeyextends Object 表示 SelectableChannel 在 Selector 中的注册的标记. 每次向选择器注册通道时就会创建一个选择键.通过调用某个键的 cancel 方法.关闭其通道,或者通过

SelectableChannel API 用法

java.nio.channels 类 SelectableChannel java.lang.Object java.nio.channels.spi.AbstractInterruptibleChannel java.nio.channels.SelectableChannel 所有已实现的接口: Closeable, Channel, InterruptibleChannel 直接已知子类: AbstractSelectableChannel public abstract class S