0

I have the following node proxy server set up to strip out the page name from a request made from one web app and use it to display page info in a second web app. It works fine but for every request intercepted a new browser is opened up. Is there anyway to detect if an internet explorer browser is aleady open so i can use that?

var http = require('http'),
  httpProxy = require('http-proxy'),
  request = require('request'),
  open = require('open');


// 
// Create proxy server  
// 
httpProxy.createProxyServer({target:'http://localhost:9000'}).listen(9085);

// 
// Create target server 
// 
http.createServer(function (req, res) {

if (req.method=='GET') {
  if (req.url.indexOf("Page.do") > -1) {

    var temp = req.url.split("Page.do")[0].split("/");
    var pageName = temp[temp.length - 1];

    var docURL = "http://localhost:9081/mysecondwebapp/pages/" + pageName +     ".html";  

    open(docURL, "iexplore");   
   };
  };

}).listen(9000);
blu10
  • 534
  • 2
  • 6
  • 28
  • You could do it using web sockets. But you'll have to inject your own JavaScript code to the page, because web socket connection should be established by web browser. After that you'll be able to send new `docURL` to already opened browser window through web socket. – Leonid Beschastny Mar 26 '15 at 16:37
  • By the way, why are you opening a browser window? Why don't you want to do something like `res.redirect(docURL)` instead? – Leonid Beschastny Mar 26 '15 at 16:41
  • hi thanks for answering, i was hoping not to have to change anything on the browser side if possible.. im not redirecting because i want to have two separate web apps open at the same time so in app 1 when the person moves around from page to page app 2 reloads to show information about that page open in app 1 (if you get what i mean).... – blu10 Mar 26 '15 at 16:52
  • Well, you could serve a simple web page with `iframe` and JS to update this `iframe` using data sent by you server using web sockets. – Leonid Beschastny Mar 26 '15 at 16:58
  • Buy the way, `open` opens a browser window on the same machine you're running your `node.js` application. Is it fine for you? I mean, you'll have to run your app on user's machine, you won't be able to move it to some remote server. – Leonid Beschastny Mar 26 '15 at 17:07
  • yeah its fine to run on same machine.... if i got it working i could share among team... as regards iframe.. this is still some client side work yeah?.. i dont really want to have to modify the app front end... – blu10 Mar 26 '15 at 17:11
  • actually would you have an example of how i might use an iframe?.. im not a front end guru but would this be a html with an iframe that displays the content of the 2nd web app?.. – blu10 Mar 26 '15 at 17:17
  • Well, I'm a back-end developer too, but I'll try. There isn't much of a front-end work here, anyway. – Leonid Beschastny Mar 26 '15 at 17:25
  • hi leonid, thanks for your help.... i have a vague idea of what you explained to me but i think with a little bit of googling i might give it a go... will give it a try later :) – blu10 Mar 26 '15 at 17:26

1 Answers1

0

Here is an iframe + socket.io solution I've talking in comments. I used express.io module because I'm more familiar with it than with original socket.io module.

Here is a server-side code:

app = require('express.io')();
app.http().io();

var base = 'http://www.w3schools.com/tags/tag_';

// Send the client html.
app.get('/', function(req, res) {
  res.sendfile(__dirname + '/client.html');
})

app.post('/:tag', function(req, res) {
  // Send new url to the all active clients
  app.io.broadcast('update', {
    url: base + req.params.tag + '.asp'
  });
  // End original request
  res.send(200);
})

app.listen(9000);

and client.html with iframe and simple JS snippet:

<!DOCTYPE html>
<html>
  <head>
    <script src="/socket.io/socket.io.js"></script>
    <style type="text/css">
      body iframe {
        overflow: hidden;
        overflow-x: hidden;
        overflow-y: hidden;
        position: absolute;
        height: 100%;
        width: 100%;
        margin: 0;
        padding: 0;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0
      }
  </style>
  </head>
  <body>
    <iframe id="frame" frameborder="0" height="100%" width="100%"></iframe>
    <script>
      io = io.connect();
      frame = document.getElementById('frame');
      io.on('update', function(data) {
        frame.src = data.url;
      });
    </script>
  </body>
</html>

Here is how it works:

  1. Start node.js server and navigate to localhost:9000 in your browser.
  2. Use curl or any other http utility to send POST request to localhost:9000/iframe.
  3. You'll see w3schools page for iframe tag displayed in previously opened window.
  4. Send another POST request to see help for some other tag.

I hope this simple example will help you.

Here are all the links I used to write it:

Community
  • 1
  • 1
Leonid Beschastny
  • 50,364
  • 10
  • 118
  • 122
  • hi leonid, thanks for this, I really appreciate the effort you went to... I will give this a try and let you know how I get on ... thanks again :-) – blu10 Mar 26 '15 at 20:35
  • I'm using [`httpie` python utility](https://github.com/jakubroztocil/httpie). But you could do it with any http utility (e.g. [curl](http://curl.haxx.se/)). If you don't familiar with any of them, you could change `app.post` to `app.get` and use your browser instead. – Leonid Beschastny Mar 27 '15 at 17:02
  • tried changing it to a get but all i get back from the browser is OK in the html, nothing included in iframe, inspecting the html it isnt the client.html... frustrating ! – blu10 Mar 27 '15 at 21:23
  • Try opening `/` in one window first. Then hit `/iframe` in the second window. Then you'll see w3school for `iframe` tag displayed in the **first** window. – Leonid Beschastny Mar 27 '15 at 21:28
  • @blu10 and [check that your browser support web sockets](http://caniuse.com/#feat=websockets). Old version of IE (prior to IE10) have no support for this technology. – Leonid Beschastny Mar 27 '15 at 21:29
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/73969/discussion-between-leonid-beschastny-and-blu10). – Leonid Beschastny Mar 27 '15 at 21:35