joke.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. var express = require('express');
  2. var request = require('request');
  3. var router = express.Router();
  4. // 创建 application/x-www-form-urlencoded 编码解析
  5. var bodyParser = require('body-parser');
  6. var urlencodedParser = bodyParser.urlencoded({ extended: false });
  7. var params = {
  8. key: '64a40e3c55e88cc8cd66a78d030bddce',
  9. method: 'get',
  10. page: 1,
  11. pagesize: 1,
  12. sort: '',
  13. time: '',
  14. type: ''
  15. };
  16. /**
  17. * Get 请求
  18. */
  19. router.get('/', function(req, res, next) {
  20. params.method = 'get';
  21. params.page = req.query.page || 1;
  22. params.pagesize = req.query.pagesize || 1;
  23. params.sort = req.query.sort || '';
  24. params.time = req.query.time || '';
  25. params.type = req.query.type;
  26. getJOKE(req, res, next);
  27. });
  28. /**
  29. * 随机获取
  30. */
  31. router.get('/rand', function(req, res, next) {
  32. params.method = 'get';
  33. params.type = req.query.type || 'pic';
  34. getJOKE(req, res, next, 'rand');
  35. });
  36. /**
  37. * Post 请求
  38. */
  39. router.post('/', urlencodedParser, function(req, res, next) {
  40. params.method = 'post';
  41. params.page = req.body.page || 1;
  42. params.pagesize = req.body.pagesize || 1;
  43. params.sort = req.body.sort || '';
  44. params.time = req.body.time || '';
  45. params.type = req.body.type;
  46. getJOKE(req, res, next);
  47. });
  48. /**
  49. * 随机获取
  50. */
  51. router.post('/rand', urlencodedParser, function(req, res, next) {
  52. params.method = 'post';
  53. params.type = req.body.type || 'pic';
  54. getJOKE(req, res, next, 'rand');
  55. });
  56. /**
  57. * 统一的请求
  58. */
  59. function getJOKE(req, res, next, op) {
  60. var url = '';
  61. if (!!op && op === 'rand') {
  62. if (params.type !== 'pic') {
  63. params.type = null;
  64. }
  65. url = "http://v.juhe.cn/joke/randJoke.php?key=" + params.key;
  66. if (!!params.type) {
  67. url += "&type=" + params.type;
  68. }
  69. } else {
  70. url = "http://japi.juhe.cn/joke/";
  71. if (!!params.type && params.type === 'text') {
  72. url += "content/text.from?key=";
  73. } else {
  74. url += "img/text.from?key=";
  75. }
  76. url += params.key + "&page=" + params.page + "&pagesize=" + params.pagesize;
  77. if (!!params.sort && !!params.time) {
  78. url += "&sort=" + params.sort + "&time=" + params.time;
  79. url = url.replace(/text/, 'list');
  80. }
  81. }
  82. request(url, function(err, response, body) {
  83. body = JSON.parse(body);
  84. if (!err && response.statusCode === 200 && body.error_code === 0) {
  85. var output = {
  86. data: body.result.data || body.result,
  87. status: {
  88. code: 200,
  89. message: ''
  90. }
  91. };
  92. if (req.query.callback) {
  93. return res.jsonp(output);
  94. } else {
  95. return res.json(output);
  96. }
  97. } else {
  98. var error = {
  99. data: {},
  100. status: {
  101. code: -1,
  102. message: body.reason
  103. }
  104. };
  105. res.json(error);
  106. }
  107. });
  108. }
  109. module.exports = router;