How can I set a default Port number?

November 14, 2017 495 views
Node.js Ubuntu

I am not using nginx or apache on my application. When I try to get to my domain "mfall33.tk" it "refuses to connect" although I can access my app through my ip address. I think that becase I cant specify my IP Address with my app that the domain can't access it, is there anyway I could go about specifying it?

1 Answer

Without more information about you application, it's hard to give you a through answer to your questions. Generally, the port that an application listens on is defined by the application itself in some way. That could be an environmental variable or a run time configuration flag.

Take this simple Express app example:

#!/usr/bin/env node
var debug = require('debug')('my-application');
var app = require('../app');

app.set('port', process.env.PORT || 3000);

var server = app.listen(app.get('port'), function() {
  debug('Express server listening on port ' + server.address().port);
});

It defaults to using port 3000 unless the PORT environment variable is set to a different value.

Assuming you have an A record for mfall33.tk pointing to the IP address of your server, to reach the app by visiting the domain it must be listing on port 80 for HTTP or port 443 for HTTPS. This is one of the reasons people put Nginx or Apache in front of their app. The web server can listen to both, redirect one to the other if desired, and proxy the application. So even if you are able to configure the port for your application, it's still likely worthwhile to put a revers proxy in front of it. Check out this tutorial for an example:

Node.js is an open source JavaScript runtime environment for easily building server-side and networking applications. Node.js applications can be run at the command line but this guide focuses on running them as a service using PM2, so that they will automatically restart on reboot or failure, and can safely be used in a production environment.
Have another answer? Share your knowledge.