const http = require('http');  
const express = require('express');
const app = express();  

const port = process.env.PORT || 3031;

//webpack
const webpack = require('webpack');  
const webpackMiddleware = require('webpack-dev-middleware');
const webpackCfg = require('./webpack.config.js');
const compiler = webpack(webpackCfg);

const publicPath = "/resources";

app.use(webpackMiddleware(compiler, {
	publicPath: publicPath,
	lazy: false,
	watchOptions: {
		aggregateTimeout: 1000,
		poll: false
	},
	stats: {
		colors: true
	},
	hot: true
}));

//hot reload
const webpackHotMiddleware = require('webpack-hot-middleware');
app.use(webpackHotMiddleware(compiler, {
	publicPath: publicPath
}));


/*
const proxyDelay = function (req, res, next) {
  if (req.originalUrl.indexOf('/api') > -1) {
    setTimeout(next, 2000);
  } else {
    next();
  }
};

*/
const proxy = require('http-proxy-middleware');
app.use('/', /*proxyDelay,*/ proxy({
	target: 'http://localhost:7071',

	onProxyReq(proxyReq, req, res) {
		//proxyReq.setHeader('X-Disable-RouterFilter', 'true');
	}

}));


//start server
console.log("Starting server in " + process.env.NODE_ENV + " mode");
app.listen(port, function() {
	console.log("Server listening on port " + port);	
});  

