joke.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. var express = require('express');
  2. var request = require('superagent');
  3. var router = express.Router();
  4. var key = '64a40e3c55e88cc8cd66a78d030bddce';
  5. //var cookie = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36' };
  6. /**
  7. * Get 请求
  8. */
  9. router.get('/', function(req, res, next) {
  10. getJOKE(req, res, next);
  11. });
  12. /**
  13. * 随机获取
  14. */
  15. router.get('/rand', function(req, res, next) {
  16. getJOKE(req, res, next, 'rand');
  17. });
  18. /**
  19. * Post 请求
  20. */
  21. router.post('/', function(req, res, next) {
  22. getJOKE(req, res, next);
  23. });
  24. /**
  25. * 随机获取
  26. */
  27. router.post('/rand', function(req, res, next) {
  28. getJOKE(req, res, next, 'rand');
  29. });
  30. /**
  31. * 统一的请求
  32. */
  33. function getJOKE(req, res, next, op) {
  34. var page = req.query.page || req.body.page || 1;
  35. var pagesize = req.query.pagesize || req.body.pagesize || 1;
  36. var sort = req.query.sort || req.body.sort;
  37. var time = req.query.time || req.body.time;
  38. var type = req.query.type || req.body.type || 'pic';
  39. var callback = req.query.callback || req.body.callback;
  40. var url = '';
  41. if (!!op && op === 'rand') {
  42. if (type !== 'pic') {
  43. type = null;
  44. }
  45. url = "http://v.juhe.cn/joke/randJoke.php?key=" + key;
  46. if (!!type) {
  47. url += "&type=" + type;
  48. }
  49. } else {
  50. url = "http://japi.juhe.cn/joke/";
  51. if (!!type && type === 'text') {
  52. url += "content/text.from?key=";
  53. } else {
  54. url += "img/text.from?key=";
  55. }
  56. url += key + "&page=" + page + "&pagesize=" + pagesize;
  57. if (!!sort && !!time) {
  58. url += "&sort=" + sort + "&time=" + time;
  59. url = url.replace(/text/, 'list');
  60. }
  61. }
  62. var output = {
  63. data: {},
  64. status: {
  65. code: 200,
  66. message: ''
  67. }
  68. };
  69. request.get(url).end(function(err, response) {
  70. var body = {};
  71. if (response && response.text) {
  72. body = response.text;
  73. } else if (response && response.body) {
  74. body = response.body;
  75. }
  76. if (typeof body === 'string') {
  77. try {
  78. body = JSON.parse(body);
  79. } catch (e) {
  80. output.status = {
  81. code: -1
  82. };
  83. }
  84. }
  85. output.data = (body.result && body.result.data ? body.result.data : body.result) || {};
  86. if (!err && response.status === 200 && body.error_code === 0) {
  87. //
  88. } else {
  89. output.status = {
  90. code: -1,
  91. message: err || body.reason || 'Something bad happend.'
  92. };
  93. }
  94. if (callback) {
  95. return res.jsonp(output);
  96. } else {
  97. return res.json(output);
  98. }
  99. });
  100. }
  101. module.exports = router;