porting the phantomjs server code into the middleware