// @flow

import express from 'express'
import url from 'url'
import webpackConfig from '../webpack/webpack.config.dev'

if (process.env.USE_DOTENV) require('dotenv').config()
const {PORT} = process.env
if (PORT == null) throw new Error("Missing process.env.PORT")

const app = express()

const compiler = require('webpack')(webpackConfig)
app.use(require('webpack-dev-middleware')(compiler, webpackConfig.devServer || {}))
app.use(require('webpack-hot-middleware')(compiler))

const proxy = require('http-proxy').createProxyServer()
proxy.on('error', (err: Error): any => console.error(err.stack))

const target = `http://localhost:${PORT}`

app.all('*', (req: Object, res: Object): any => proxy.web(req, res, { target }))

const server = app.listen(webpackConfig.devServer.port)

server.on('upgrade', (req: Object, socket: any, head: any) => {
  const parsed = url.parse(req.url)
  if (parsed && parsed.pathname && /^\/sockjs/.test(parsed.pathname)) {
    proxy.ws(req, socket, head, { target })
  }
})

console.log(`Dev server is listening on http://0.0.0.0:${webpackConfig.devServer.port}`)

@@ -37,30 +37,8 @@
 ss'%0A
-import url from 'url'%0A
 impo
@@ -818,122 +818,8 @@
 ) =%3E
- %7B%0A  const parsed = url.parse(req.url)%0A  if (parsed && parsed.pathname && /%5E%5C/sockjs/.test(parsed.pathname)) %7B%0A   
  pro
@@ -858,14 +858,8 @@
 t %7D)
-%0A  %7D%0A%7D
 )%0A%0Ac

remove unnecessary check before proxy.ws