node.js - Changing localhost server files are served from using Node (Webstorm /maybe IntelliJ) -
i'm not sure i'm missing here, can me out. i'm working on project we're using node , in run/edit configurations i've down following:
node interpreter:
path node.exe file checked out subversion
working directory:
"app.js" file is, path command line type node app.js , starts server
javascript file:
app.js name of file creates server
now main nav bar when run / run server box @ bottom pops , tells me express server listening on port 3000. cool.
i can navigate localhost:3000/mypage.html , can page fine.
added json file same directory on hard drive mypage.html in, , can navigate localhost:3000/largetestdata.json
.
so server , running , serving file should. problem in webstorm project, want make ajax request largetestdata file. using jquery like:
var data = $.get('localhost:3000/largetestdata.json'); data.done(function(data){ console.log('here data'); cnosole.log(data); })
when error (in chrome)
xmlhttprequest cannot load localhost:3000/largetestdata.json. cross origin requests supported http.
and @ url , i'm seeing:
http://localhost:63342/
obviously webstorm has started server correctly, when view html file, it's not using server (which, of course why i'm getting cors error.
there's fundamental stuff here i'm not getting. need ide deploy web server started up, it's not doing that. please, give me once on over technologies i'm missing out on here.
webstrom didn't start node.js server, serves static pages own internal http server doesn't know node.js , express.
the main problem:
when start node.js server, it's serving json files on port 3000. if open html-page little menu in webstorm (where can choose browser), webstorm opens browser url pointing own internal webserver running on different port (e.g. 63342). javascript security prohibits loading data different host/port same-origin policy.
it's not webstorm's fault , need solution problem in production or can't go live.
general solution:
either have ensure html pages , json data come same host+port, or can circumnavigate (a) setting server-side headers ('access-control-allow-origin: *') @lena suggested, or (b) using jsonp. below find thoughts using nginx reverse proxy browser's point of view requests go same host+proxy. it's common solution, mentioned above, there other options.
primitive solution:
don't use webstorm open browser. load page http://localhost:3000/
, change url of rest resource $.get('/largetestdata.json')
. you'll miss comfort ide, can see program working.
comfortable solution:
as @lena suggested, there way configure express/node.js server known webstorm. haven't tried it, suppose can press run-button , maybe node.js plugin in webstorm intelligent know static-maps in express , know how map html-file web application url , open page in browser url served node.js application. (i'd surprised once again if works magically, maybe can configure mapping files urls manually, don't know.)
dirty solution
with options can disable security checks, @ least in google chrome. it's possible load json data different port html page. wouldn't recommend using these options (just opinion).
additional hints
if more playing around node.js , ui fun , have serve application "production-ready", have @ nginx serve static files , reverse proxy node.js requests there. i'm using setup development , works charm.
of course node.js / express able serve static files well, imo placing nginx in front of node.js (clustered) bring bunch of advantages production sites, e.g. load-balancing, ssl-offloading, avoid jsonp, in many cases performance, easier deployment updates, availability.
Comments
Post a Comment