How to Select a Static Port Number for a Custom App

How to select a static port number for a custom app?

If you can't predict the exact kind of environment your application is going to run, just don't bother with this. Pick any number over 1024 and also make it configurable so the user can change it in case of conflict with another service/application.

Of course you can still avoid very common ports like 8080 (alternative HTTP) or 3128 (proxies like squid), 1666 (perforce), etc. You can check a comprehensive list of known ports here, or take a look at /etc/services.

Which low-number network ports required in iOS apps?

You can check this: TCP and UDP ports used by Apple software products

As it's written at the above article, Many of these ports are well-known, industry-standard ports. You should use a known port only for its purpose (for example - use port 443 only for https).

If you have your own custom protocol, use a port that isn't one of the common ports. For that - choose a big number. You might also run a test to check that the port you chose is not already been used.

In addition - see this thread
How to select a static port number for a custom app?

And finally, you can also check here, where you can see a lot of port numbers usage: https://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xhtml?&page=1

How to specify a port number while running flutter web

I found the answer inside the flutter_tools source code:

flutter run -d headless-server --web-port=1234

How to set new port while serving application through static-server in React JS

You should give the port to your process, as an argument, not as an environment variable, like:

static-server -p 8080

Some notes on setting environment variables

If you're in a UNIX-based Operating system link macOS or Linux, there are multiple ways to set environment variables:

  1. If you want the variable to be only set for the latter command, you
    should use it this way

    PORT=8080 npm run dev.

  2. If you want to set the variable for the entire terminal session, you should set it this way

    $ set -a
    $ PORT=8080
    & yarn run dev

  3. You can add it to your ~/.zshrc / ~/.bashrc ~/.profile to have it always in your terminal:

    export PORT=8080

How to specify a port number in a create-react-app based project in production?

The server you start with npm start command in create-react-app is webpack dev server. It only meant to use while development and you shouldn't use that in a production environment.

Instead, you can easily host a CRA app with a simple static file server which can easily configure with nginx without a reverse proxy or changing the port of dev server. You simply need run npm run build command and deploy content of build folder to the appropriate folder of your static file server. You can read more about this in the documentation of CRA.

Also, make sure that you specify the homepage in your package.json before building the project, since you are going to host your app in a relative path like mywebsite.com/project1. Because CRA assumes your app is hosted at the server root with default settings.

Hope this helps!

How to specify the port an ASP.NET Core application is hosted on?

In ASP.NET Core 3.1, there are 4 main ways to specify a custom port:

  • Using command line arguments, by starting your .NET application with --urls=[url]:
dotnet run --urls=http://localhost:5001/
  • Using appsettings.json, by adding a Urls node:
{
"Urls": "http://localhost:5001"
}
  • Using environment variables, with ASPNETCORE_URLS=http://localhost:5001/.
  • Using UseUrls(), if you prefer doing it programmatically:
public static class Program
{
public static void Main(string[] args) =>
CreateHostBuilder(args).Build().Run();

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(builder =>
{
builder.UseStartup<Startup>();
builder.UseUrls("http://localhost:5001/");
});
}

Or, if you're still using the web host builder instead of the generic host builder:

public class Program
{
public static void Main(string[] args) =>
new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.UseUrls("http://localhost:5001/")
.Build()
.Run();
}

How can I change IIS Express port for a site

To specify a port for a Web application project that uses IIS Express

  1. In Solution Explorer, right-click the name of the application and then select Properties.
    Click the Web tab.

  2. In the Servers section, under Use Local IIS Web server, in the Project URL box change the port number.

  3. To the right of the Project URL box, click Create Virtual Directory, and then click OK.

  4. In the File menu, click Save Selected Items.

  5. To verify the change, press CTRL+F5 to run the project.
    The new port number appears in the address bar of the browser.

From How to: Specify a Port for the Development Server (archive.org backup here).

How to set port for express server dynamically?

How to set port for express without needing to hardcode or even choose port yourself?


###Option 1: Environment variable (recommended)

Often times you will deploy your app to a hosting provider like Heroku. Depending on how the host is configured, the system will dynamically set an environment variable and your app will need to get the port from this variable. For example, the hosting provider might run a command like this when it runs your app:

$ PORT=1234 npm start

... and within your code, you can access this variable like this:

const port = process.env.PORT;
app.listen(port);

Pro tip: Most hosting providers let you define custom environment variables too. You can test this locally by creating arbitrary variables like this:

$ FOO=bar ADMIN_EMAIL=joe@example.com npm start

...and access those variables from code like this:

const foo = process.env.FOO;                  //-> "bar"
const adminEmail = process.env.ADMIN_EMAIL; //-> "joe@example.com"


Option 2 - environment-specific config files (also highly recommended)

Using a config library like config and/or dotenv allows you to easily manage environment-specific config options. Your folder structure would look like this (note the names of the files):

|- config
|- default.json
|- testing.json
|- production.json
|- src
|- app.js

You then define your "default" variables and environment-specific variables:

default.json

{
"port": "3030",
"adminEmail": "dev@example.com"
}

testing.json

{
"port": "5555"
}

production.json

{
"adminEmail": "admin@example.com"
}

The config library will always use the default variables. When you are on testing it will use the default admin email and a different port. When you are on production it will use the default port but a different admin email. The way you define your "node environment" is like this (notice we use the same name as the JSON config files):

$ NODE_ENV=testing npm start
$ NODE_ENV=production npm start

Pro tip: Your configuration files can reference environment variables too! Continuing with our example from Option 1 above, you can define your production config like this:

production.json

{ 
"port": "PORT"
}

The config library will look for any environment variables named "PORT" and will use that value. Putting it all together, your final command to run your app might look like this:

$ NODE_ENV=production PORT=47861 npm start

Pro tip: - dotenv can be used alongside the config library for ultimate environment variable management!!



2. What is the difference between using app.set('port', portNum) and directly using port number in app.listen(portNum)?


Express allows you to set application variables using app.set - but this is just a fancy way for defining variables. You can later get the values for these variables using app.get.

Sooner or later, you are going to need to tell your app to listen for traffic on a specific port. You could do something like this:

const app = express();
app.set('port', process.env.PORT);

app.use((req, res) => { ... });
app.listen(app.get('port'));


Related Topics



Leave a reply



Submit