server.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. var fs = require('fs');
  2. var request = require('request');
  3. var connect = require('connect');
  4. var serveStatic = require('serve-static');
  5. var bodyParser = require('body-parser');
  6. var config = require('./config.json');
  7. var apiEndpoint = 'https://www.cloudflare.com/api_json.html';
  8. var isProd = config.isProd === undefined || config.isProd;
  9. var app = connect();
  10. var serve = serveStatic('.', {'index': []});
  11. var serveIndex = function(req, res, next) {
  12. if(isProd) {
  13. serve(req, res, next);
  14. }
  15. else {
  16. var html = fs.readFileSync('./index.html', {encoding: 'utf8'});
  17. res.setHeader('Content-Type', 'text/html; charset=utf8');
  18. res.end(html.replace('/assets/bundle.js', 'http://localhost:8001/assets/bundle.js'));
  19. }
  20. };
  21. app.use(bodyParser.urlencoded({extended: false}));
  22. app.use(function(req, res, next) {
  23. if(req.url === '/api') {
  24. req.body.email = config.email;
  25. req.body.tkn = config.token;
  26. // filter out only zones in the whitelist
  27. if(req.body.a === 'zone_load_multi') {
  28. request.post({uri: apiEndpoint, form: req.body, json: true}, function(err, inc, body) {
  29. var filtered = body.response.zones.objs.filter(function(zone) {
  30. return config.whitelist.indexOf(zone.zone_name) >= 0;
  31. });
  32. body.response.zones.objs = filtered;
  33. body.response.zones.count = filtered.length;
  34. res.setHeader('Content-Type', 'application/json');
  35. res.end(JSON.stringify(body));
  36. });
  37. }
  38. // allow any requests for zones in whitelist
  39. else if(config.whitelist.indexOf(req.body.z) >= 0) {
  40. request.post(apiEndpoint).form(req.body).pipe(res);
  41. }
  42. // deny otherwise
  43. else {
  44. next();
  45. }
  46. }
  47. else {
  48. next();
  49. }
  50. });
  51. app.use(serve);
  52. app.use(serveIndex);
  53. if(!isProd) {
  54. var WebpackDevServer = require('webpack-dev-server');
  55. var HotModuleReplacementPlugin = require('webpack/lib/HotModuleReplacementPlugin');
  56. var webpack = require('webpack');
  57. var webpackConfig = require('./webpack.config.js');
  58. webpackConfig.entry = [
  59. "webpack-dev-server/client?http://localhost:8001",
  60. "webpack/hot/dev-server",
  61. webpackConfig.entry
  62. ];
  63. webpackConfig.output.path = '/';
  64. webpackConfig.output.publicPath = 'http://localhost:8001/assets/';
  65. webpackConfig.plugins = webpackConfig.plugins || [];
  66. webpackConfig.plugins.push(new HotModuleReplacementPlugin());
  67. webpackConfig.devtool = 'eval';
  68. var devServer = new WebpackDevServer(webpack(webpackConfig), {
  69. contentBase: 'http://localhost:8000',
  70. publicPath: webpackConfig.output.publicPath,
  71. hot: true
  72. })
  73. devServer.listen(8001);
  74. }
  75. app.listen(8000);