2

I'm not sure what I'm missing here, so hopefully someone can help me out. I'm working on a project where we're using Node and in the Run/Edit configurations I've down the following:

Node interpreter: This is the path to the node.exe file which I checked out from Subversion

Working directory: this is where the "app.js" file is, this is the path that from the command line you type node app.js and it starts the server

JavaScript file: app.js This is the name of the file that actually creates the server

Now from the main nav bar when I do Run / Run my server the box at the bottom pops up and tells me that Express server is listening on port 3000. Cool.

I can navigate to localhost:3000/myPage.html and I can get to the page just fine.
I added as JSON file to the same directory on my hard drive that myPage.html is in, and I can navigate to that as well by localhost:3000/largeTestData.json.

So the server is up and running and serving file as it should. My problem is that in my Webstorm project, I want to make an AJAX request to that largeTestData file. I do so using jQuery like:

 var data = $.get('localhost:3000/largeTestData.json');
    data.done(function(data){
        console.log('here is your data');
        cnosole.log(data);
    })

When I do that I get the error (in Chrome)

XMLHttpRequest cannot load localhost:3000/largeTestData.json. Cross origin requests are only supported for HTTP. 

and so I look at the URL and I'm seeing:

http://localhost:63342/

Obviously Webstorm has started the server correctly, but when I view an HTML file, it's not using that server (which, of course is why I'm getting the CORS error.

There's some fundamental stuff here which I'm obviously not getting. I need my IDE to deploy to the Web server that it started up, but it's not doing that. Please, someone give me a once over on all the technologies that I'm missing out on here.

wootscootinboogie
  • 7,688
  • 33
  • 99
  • 182
  • How do you open your HTML? Do you get this error on running your code, or when opening your HTML page in browser? – lena May 30 '14 at 12:31
  • actually I have no idea what this issue has to do with WebStorm. You can try to run Chrome with --disable-web-security (http://stackoverflow.com/questions/3102819/chrome-disable-same-origin-policy). Or change your server code so that it responds with the 'Access-Control-Allow-Origin: *' header – lena May 30 '14 at 12:57
  • @lena I use the little bar that Webstorm gives you when you're in an HTML page. If you move your mouse to the upper right hand corner it offers you the option to view the file in Chrome, Firefox, etc. When you do this, it creates its own little webserver, I would like for it to deploy to the running node server. – wootscootinboogie May 30 '14 at 12:59
  • it neither creates a webserver on file opening nor deploy it anywhere - it just opens your HTML in webstorm built-in webserver that starts on starting webStorm. If you want your pages being opened on a different server by default, you need to configure this server in Settings/Deployment and make it default for your project. But this has ABSOLUTELY nothing to do with your cross-origin issue – lena May 30 '14 at 13:35

3 Answers3

3

WebStrom didn't start your node.js server, but serves static pages by its own internal HTTP server which doesn't know anything about node.js and Express.

The main problem:

When you start your node.js server, it's serving JSON files on port 3000. If you open an HTML-page with the little menu in WebStorm (where you can choose the browser), WebStorm opens the browser with an URL pointing to its own internal webserver running on a different port (e.g. 63342). JavaScript security prohibits loading data from a different host/port Same-origin policy.

It's not WebStorm's fault and you need a solution for this problem in production or you can't go live.

General Solution:

Either you have to ensure that HTML pages and JSON data come from the same host+port, or you can circumnavigate with (a) setting server-side headers ('Access-Control-Allow-Origin: *') as @lena suggested, or (b) using JSONP. Below you find some thoughts using nginx as a reverse proxy so from browser's point of view all requests go to the same host+proxy. It's a very common solution, but as mentioned above, there are other options.

Primitive solution:

Don't use WebStorm to open your browser. Load the page from http://localhost:3000/ and change the URL of the REST resource to $.get('/largeTestData.json'). You'll miss some comfort from your IDE, but you can immediately see that your program is working.

Comfortable solution:

As @lena suggested, there is a way to configure your Express/node.js as a server known to WebStorm. I haven't tried it, but I suppose you can then just press the Run-button and maybe the node.js plugin in WebStorm is as intelligent to know the static-maps in Express and know how to map an HTML-file to a web application URL and open the page in the browser with the URL served by your node.js application. (I'd be surprised once again if this really works magically, but maybe you can configure a mapping from files to URLs manually, I don't know.)

Dirty solution

With some options you can disable security checks, at least in Google Chrome. Then it's possible to load JSON data from a different port than your HTML page. I wouldn't recommend using these options (just my opinion).

Additional Hints

If you do more than just playing around with node.js and some UI fun and you have to serve your application "production-ready", then have a look at nginx to serve your static files and reverse proxy node.js requests from there. I'm using this setup even for development and it works like a charm.

Of course node.js / Express is able to serve static files as well, but IMO placing something like nginx in front of node.js (clustered) bring a bunch of advantages for production sites, e.g. load-balancing, ssl-offloading, avoid JSONP, in many cases performance, easier deployment updates, availability.

hgoebl
  • 11,224
  • 7
  • 39
  • 67
  • this issue is exactly the one mentioned in my answer - wrong URL format; and it has no relation to WebStorm; not using WebStorm is not a solution - it's not a problem to start node server and load the page from correct URL from WebStorm – lena Jun 02 '14 at 18:13
  • @lena I edited my question. Please tell me, if something is still missing or wrong. Maybe you could point the OP to some screenshots how to configure WebStorm to load pages from node and not internal web server from WebStorm? – hgoebl Jun 02 '14 at 19:39
  • do you mean loading for page previewing (using Alt+F2, or using the browsers bar in the editor)? You have to configure your node server as a default web server in Settings/Deployment. See http://wiki.jetbrains.net/intellij/Remote_JavaScript_debugging_with_WebStorm_and_PHPStorm#Configuring_access_to_the_Web_server, http://confluence.jetbrains.com/display/PhpStorm/Deploying+PHP+applications+with+PhpStorm. But note that your node server should be up and running to make this work! WebStorm won't start it for you automatically – lena Jun 03 '14 at 11:39
  • And, of course, you can configure your Run configuration to open the correct URL when your node server is started. This is what the 'Browser/LiveEdit' tab of the Node.js Run configuration is supposed to be used for. Check both 'After launch' and 'with javaScript Debugger' options there, specify the URL of your Node server your front end is served on (http://localhost:3000, for example) and then run your Node.js application - it will start the server and then open your client page in browser, allowing to live edit your HTML/CSS – lena Jun 03 '14 at 11:46
0

To get your code working, just change the URL in $.get() to full URL (including protocol):

var data = $.get('http://localhost:3000/phones.json');
lena
  • 73,196
  • 6
  • 110
  • 121
0

In Webstorm 2016.3 (and probably earlier) there is now another option. Under the Configuration Settings for NodeJS runs, one can manually set the page and port to be loaded via Webstorm's "Browser/Live Edit" settings.

See the screenshot below for settings one can change.

enter image description here

prestonsmith
  • 660
  • 1
  • 9
  • 29