npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

res.api

v1.0.11

Published

res.api is an express middleware for render json api , it convention over api format

Downloads

18

Readme

res.api

npm version

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

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:最通用的api接口

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:返回成功带有状态的json数据

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:最常用的成功返回json数据

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": "request success!"
  }
}

way 4:快捷错误处理

return res.api_error(err);

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": {
  },
  "status": {
    "code": -1,
    "msg": "api error"
  }
}

配置项

  • res.api_error_code; 默认是200
  • res.api_error_status_code; 默认是-1
  • res.api_error_status_msg; 默认是'api error'

return res.api_error(err);之前设置

res.api_error_code = 400;
return res.api_error(err);

the response header is :

HTTP/1.1 400 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": {
  },
  "status": {
    "code": -1,
    "msg": "api error"
  }
}

jsonp support

res.is_jsonp = true
return res.api(data);

video

录了一段7分钟的视频:

http://pan.baidu.com/s/1eQo4xqi

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Version History

  • v1.0.11 支持自定义api_error信息
  • v1.0.6 add api_error()
  • v1.0.0 初始化版本

欢迎fork和反馈

如有建议或意见,请在issue提问或邮件

License

this repo is released under the MIT License.