v1.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. var express = require('express');
  2. var request = require('request');
  3. var router = express.Router();
  4. router.all('/v1/*', function(req, res, next) {
  5. res.header("Access-Control-Allow-Origin", "*");
  6. res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  7. res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
  8. res.header("X-Powered-By", '3.2.1');
  9. res.header("Vary", "Origin");
  10. res.header("Content-Type", "application/json;charset=utf-8");
  11. next();
  12. });
  13. router.get('/', function(req, res, next) {
  14. var protocol = req.protocol;
  15. var host = req.host;
  16. var ip = req.ip;
  17. if (req.originalUrl == /v1/) {
  18. return res.send({
  19. info: 'Please Set URL Like This: ' + protocol + '://' + host + '/v1/?url=http[s]://YourWantProxyUrl.com'
  20. });
  21. }
  22. var url = req.originalUrl.replace('/v1/?url=', '');
  23. url = url.indexOf('?') === -1 ? url.replace('&', '?') : url;
  24. console.log('ref:' + req.get('reference'));
  25. console.log('path:' + req.path);
  26. console.log(req.subdomains);
  27. console.log(url);
  28. getJSON(url, function(data) {
  29. if (req.query.callback) {
  30. return res.jsonp(data);
  31. } else {
  32. return res.send(data);
  33. }
  34. });
  35. });
  36. function getJSON(url, callback, next) {
  37. request(url, function(err, res, body) {
  38. if (!err && res.statusCode == 200) {
  39. callback && callback(body);
  40. } else {
  41. console.log(err);
  42. }
  43. });
  44. }
  45. module.exports = router;