Quick tip for Cordova and the Browser Platform – Setting a Custom Port
How to change ports to set up CORS in a Cordova app, including some useful images to help you.
Join the DZone community and get the full member experience.
Join For Free
i’m not regularly using cordova and the browser platform because most of the time i’ll use
ionic serve
instead. however, last night i was working on a cordova project that – shockingly – didn’t use ionic. i needed to run it in a web page to do some quick testing. i quickly discovered that one thing the browser platform does not handle is enabling cors for all requests. i had set up cors for the server part of this application a while ago but it required me to use localhost:3333. by default, cordova will use port 8000 for the port. there wasn’t an obvious way to change that so i did some digging.
the first thing i did was go into the browser folder under platforms. under
platforms/browser/cordova
i opened up the
run
file and saw that the script did include the ability to pass in a port argument. i couldn’t figure out how to pass it though so i tried doing
run -h
:
ok, that’s simple. i confirmed it worked by doing
cordova/run --port=3333
. sweet. but how do it via the “main” cordova cli? if you run
cordova help run
, you’ll see this nugget in the docs:
so basically, this is all you need to do:
cordova run browser -- --port=3333
. simple, right? probably everyone but me knew this, but as i had to dig to figure it out, i thought it made sense to blog it.
Published at DZone with permission of Raymond Camden, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments