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

Launching an app doesn’t need to be daunting. Whether you’re just getting started or need a refresher on mobile app testing best practices, this guide is your resource! Brought to you in partnership with Perfecto

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.

Keep up with the latest DevTest Jargon with the latest Mobile DevTest Dictionary. Brought to you in partnership with Perfecto.

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.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}