0

Say I wanted to write a local web server that listened on a random port.

Now I want to write HTML that works in most browsers that can do AJAX calls to it.

Is there any fundamental limitation here?

EDIT: I've confused people - probably because this is almost the first time I've ever posted with the javascript tag and I don't have a feel for how to ask questions.

I recently became aware of node.js - which I didn't learn more than it makes writing stand alone web servers easy to implement.

Then I had the vision of a site sort of like this one (any programming board really) but where code samples could execute on end users local machine. I don't like browser plugins at all though, so I started trying to think of a way to make it happen with minimal knowledge between the web/browser part and the local machine's service (there has to be interaction - i just wanted to think of the minimal amount).

And that is just an example - really I write intranet business apps for a living (in which case defining well known ports is easier so I don't need an answer to this question for that purpose..)

Aaron Anodide
  • 16,906
  • 15
  • 62
  • 121
  • I think this need to be closed as unconstructive. – Gabriel Santos Apr 14 '12 at 21:54
  • why is that? because nobody would ever want to do it or it's a stupid question? i'll delete it myself if you let me know why – Aaron Anodide Apr 14 '12 at 21:55
  • 2
    How do you get that piece of html/javascript to the browser in the first place if your server is listening on a random port? – Mat Apr 14 '12 at 21:56
  • i was thinking of HTML/JS that was aware of the existance of a webserver on localhost but doesn't know what port it is listening on and didn't need elevated permissions – Aaron Anodide Apr 14 '12 at 21:58
  • Maybe you could write a WebSocket port scanner ;) – Jonatan Apr 14 '12 at 21:58
  • Sorry misunderstand your question. I think your question title is your real question. – Gabriel Santos Apr 14 '12 at 21:59
  • Does node.js not work for you (or is a JS-enabled web server not the requirement)? – Jared Farrish Apr 14 '12 at 22:00
  • i just became aware of the existance of node.js recently, and that is in part why i'm asking this. i like the idea of local applications assisting web applications – Aaron Anodide Apr 14 '12 at 22:00
  • 3
    What does this even mean? The Javascript just tries to connect to every port to see if the server is running there? – jli Apr 14 '12 at 22:01
  • What people are going to have problems with are the routing; do you a "browser-supported" server with a route-through from the request to a desktop (and back), or a server with a JS enabled response? – Jared Farrish Apr 14 '12 at 22:04
  • i think the reality is i'm a relative beginner to this particular nieche of app development... – Aaron Anodide Apr 14 '12 at 22:07
  • Your moxie has outpaced your knowledge. Try [TIBCO General Interface](http://developer.tibco.com/gi/default.jsp). The capabilities of what the browser can offer is really interesting, but you need some experience to understand why you're not quite the tip of the spear here. – Jared Farrish Apr 14 '12 at 22:15

1 Answers1

0

The same origin policy will prevent you from accessing other ports (at least in most browsers), but you I guess you could send JSONP requests to every port on localhost, and then catch the port that gives a valid response.

...If you really wanted to, that is. There is a lot of valid ports, so it could take some time.

AHM
  • 5,145
  • 34
  • 37
  • just thought of this - on windows at least - isn't there some text file that trumps all other DNS resolution... the hosts file maybe? could i have my local service modify that, then have something like a SJONP to the new entry? – Aaron Anodide Apr 14 '12 at 22:11
  • Yeah, windows have a hosts file like most other OSses (http://en.wikipedia.org/wiki/Hosts_%28file%29) but I don't think you can do routing based on ports with the hosts file. – AHM Apr 14 '12 at 22:15
  • if you could be so kind, could you give me a like so i can read your definition of routing here (i'm just starting to dabble in this area so i have some learning to do) – Aaron Anodide Apr 14 '12 at 22:16
  • The problem is that browsers enforce a "same origin policy". That is normally, you are not allowed to access any domains, that are not the same domain as the page you are viewing is from. This includes ports, so from say, http://example.com/something, you are not allowed to access http://localhost/ or http://localhost:1234. This doesn't really have anything to do with routing. You could use JSONP, as the whole point of that is to bypass the origin check. – AHM Apr 14 '12 at 22:21
  • just out of curiosity, but does "browser brew station" reference... i certainly appreciate that you guys have taken time to lend your insights... – Aaron Anodide Apr 14 '12 at 22:30
  • Aaron, I would suggest (if you're not aware) reviewing the [GoF design patterns](http://en.wikipedia.org/wiki/Design_Patterns). It's a base of understanding that really helps average communication without unnecessary duplication. – Jared Farrish Apr 14 '12 at 22:34
  • I do own it - never read it word for word - i assume if i do that i'll get your reference though, thanks – Aaron Anodide Apr 14 '12 at 22:40
  • What is it you're wanting? A browser-based computational wonderland? A server-based data store with browser-side computational front end? A data stream fed to a client-capable data computational engine? What do you want (or, honestly, **need**?) – Jared Farrish Apr 14 '12 at 22:45
  • i want (not at all need - this is the weekend here) stack overflow to have code samples that can execute on my computer with one click because a local service is running on my computer that talks to the posts i am viewing, decides if they are compilable, then executes them if i want to. the next step would be the html/js being aware it was possible for the code to execute on the remote users local machine and possibly interact with the result – Aaron Anodide Apr 14 '12 at 22:52
  • You realize what you're describing is both a client-side (push) system as well as a system to determine the client's current position in relation to the (remote) server's state? This isn't new. Having a JS-based client capable of managing of a longterm connection notification is pretty rote based on the tool's interaction with the central store is normal by now, as well as the longterm timeout techniques used to update a user's current state. (At the moment, I can't remember what this technique is called.) – Jared Farrish Apr 14 '12 at 22:59