process.env.PORT prints weird string - javascript

When running console.log("Express server listening on port " + port); in node.js on a deployed Azure website, I get the following string
Express server listening on port \\.\pipe\d9797e42-9e1f-421c-91b9-3d86496eaeb8
What is this and how can I determine what port my site is really running on?

Should be running in port 80.
Seems like an internal instruction from the server

Related

What is the significance, if any of port 9229, and can it be changed?

By default, I was taught, when I run a local Node server to run it on port 3000.
After reading a tutorial on the Chrome Node debugger which is set to inspect port 9229, I switched to this port.
Here is a similar Q / A on port 3000.
Here is the tutorial.
Can I switch the default to 3000 some how?
This way it will match my Express local port:
app.set('port', (process.env.PORT || 3000));
Or would it be more prudent to change my local express server to 9229?
I know this is a super old question in SO time but if anyone else comes here for whatever reason, here's the skinny.
TLDR; Port 3000 is for your application, Port 9229 is for the NodeJS debugger.
Port 9229 is the default NodeJS debugging port. It allows you to attach a debugging tool like Chrome's Inspector or WebStorm's Remote Debug to a Node process that is running with a special flag (node --inspect server.js in Node v8+ I think, no idea what it was before that). This has nothing to do with what port your listing to in your HTTP application.
Port 3000 is the "recommended" port to run HTTP applications like Express, Koa, Hapi, etc. I have no idea when it was decided that port 3000 was the goto but here we are.
If you want to read more about NodeJS debugging here's a link.

SocketIO is not working in Heroku Environment [Works in Localhost]

I developed chat program with NodeJS and SocketIO. Then I deployed to Heroku Server and I found that SocketIO is not working in Heroku.
I already checked the log in Heroku but there are nothing. But it is weird that it is working in localhost but not in Heroku environment. I think there are some problem with PORT Setting.
[Server Side Code]
/* Socket IO Settings */
const server = require('http').Server(express);
const io = require('socket.io')(server);
let port = process.env.PORT || 3001;
server.listen(port, function () {
console.log(`SocketIO Listening on port : ${port}`)
});
[Client Side Code]
/* Chat Functions */
var socket = io.connect(`${window.location.hostname}:3001`);
At first time I just use var socket = io.connect('http://localhost:3001'); code and it works well in Localhost Env. But after I deployed to heroku I add Heroku Variable to use it in production.
[PORT Setting]
NODEJS Server : 3000
SOCKET IO : 3001
Heroku process.env.PORT : 3001 (But When I check Heroku console it changes everytime when I reload the dynos and in Logs said me that the ports are already in use.)
I already read Heroku Document but it seems that I create NodeJS App with Express Generator I think it should be different solution. I already tried heroku document but it's not working
Set your server ip in server.listen function
var ip='127.0.0.1';
var port ='3001' ;
server.listen(ip,port, function() {console.log('server stsrt in ip: ' +ip+' and port: ' +port);}) ;

how to listen on any port on heroku when a web port 80 is already in use?

I am new to heroku and i have deployed a meteorjs application on heroku. My Meteorjs application is a webapplication, so after the build is done it runs on heroku on port 80. But simultaneously i also want to listen on a port eg:4000 or any so that i can catch my webhook events fired by any third party i want to listen to. On my local machine it runs perfectly with webapp running on a port and listener running on another, but on heroku after i deploy its just the webapp which runs and listener doesn't listen. Can anybody help me or guide me. Any new solutions are also welcome..
In my server/main.js i have made a bot instance of facebook-bot-messenger and below is the code to start the listener
bot.listen(4000);
In my client/main.html i have the html code which is just hello world
On local machine when i visit http://localhost:3000 i can see helloworld and app is also listening on port 4000.
On heroku when i visit https://appname.herokuapp.com i see helloworld . In this case the port is 80 which is fine, but the bot is not listening as any post calls i make on https://appname.heroku.com on port 4000 doesn't respond.
Heroku assigns the port for you, so you can't set your own. You will have to read in the port number from the PORT environment variable. In Node this would be process.env.PORT
You can also use || for as and OR. For example in the below case, it will read in the port number from the environment variable while on Heroku, but locally it will default to port 4000.
const app = express();
app.set('port', (process.env.PORT || 4000));
//Start Server
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});

Communicating over socket.io with a heroku app

I have two applications that need to communicate with each other. One is a node app deployed on heroku. The other is a node app deployed on a raspberry pi3. I need the raspberry pi3 to send the heroku app data using socket.io
On the heroku app I have
http.listen(process.env.PORT, function() {
console.log("listening on *:" + process.env.PORT);
});
Which listens on the port Heroku assigns.
On the raspberry pi app I have :
let socket = io(`http://appName.herokuapp.com:7256`)
I logged out the port number from heroku when it connected and it was 7256. However, I am unable to communicate with the application on Heroku. There are not any errors when I am trying to emit from the pi. But the Heroku app isn't receiving anything. Is there anything else I need to do?
Use port 80 (or no port at all, to default to 80).
The PORT env var is an internal port - it's the port within the Heroku container that your app should listen on.
Externally, Heroku's routers expose your app online at the normal ports: 80 and 443 (http and https).

I don't have to specify a port for heroku for my socket.io app?

I've successfully made a test chat app and I've gotten a node js server with socket.io running on heroku. On my local computer I have to specify the port number of localhost on the client side to the port that the server has set up. However, when I run my server code on heroku. Removing the server
I'm using the process.env.PORT variable since heroku sets that up:
var port = process.env.PORT || 3000;
http.listen(port, function(){
console.log('listening on *:' + port);
});
Naturally I find the port number that the app is running on place it in the url
var socket = io('https://xxxx.herokuapp.com:1111');
However this gives me an "net::ERR_CONNECTION_REFUSED".
I got it to work by removing the port nubmer after the url (in this example :1111). I'm wondering why this is working since most of the tutorials and articles online have it specifying the port and why my local computer needs the port to work as well.
When you connect to your https://xxxx.herokuapp.com subdomain on heroku on port 443 (which is the port that is used for an https connection when no port is specified), Heroku is probably using a proxy or router to route that incoming connection to the particular port that your node.js server is listening to. In the Heroku infrastructure, they know what actual internal host your server is running on and what actual port number it is running on so they can map a default port request on your subdomain to the actual port/host.
This is done so that browsers can connect to your subdomain directly on the default port without having to know the particulars of your node server installation and so that Heroku can auto-manage your server and likely share hardware with other customers. You are each running on a different port, but sharing the same machine. The ports are managed entirely by Heroku and this is one way that they are able to put multiple customers on the same hardware without each having to specify a custom port in the browser URL (which would be a non-starter for most customers).
So, Heroku is hosting some sort of proxy for your sub-domain that is listening to the default https port. Thus, you don't have to specify the port in the URL. Internally, they route that connection to your actual port on your actual server.
When running on your desktop, there is no such proxy to do this for you so you have to make sure client and server port numbers match.

Categories