server.js 2.8 KB

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