Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Quick tip for Cordova and the Browser Platform – Setting a Custom Port

DZone's Guide to

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.

· Mobile Zone
Free Resource

Download this comprehensive Mobile Testing Reference Guide to help prioritize which mobile devices and OSs to test against, brought to you in partnership with Sauce Labs.

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:

shot1

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:

shot2

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.

Analysts agree that a mix of emulators/simulators and real devices are necessary to optimize your mobile app testing - learn more in this white paper, brought to you in partnership with Sauce Labs.

Topics:
cordova ,mobile

Published at DZone with permission of Raymond Camden, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}