I can't understand Why I can only connect to 8080 port in node Js express server - node.js

const express = require('express');
const app = express();
app.set('port', process.env.PORT || '3000');
app.get('/', (req,res)=>{
res.send('Hello Express');
});
app.listen(app.get('port'),()=>{
console.log(app.get('port'),'listening');
})
Hi, I started studying nodeJs but got a problem.
If I run the server this way,
$ node app.js
8080 Listening
There's no problem in connecting to this server.
However, I got a problem if I run the server after changing the port.
$ PORT=3001 node app.js
3001 Listening
It seems working without problem but I can't connect to 3001 port. So I tried other ports but neither worked. I can't understand why only 8080 port is available.
I'm developing by using AWS cloud9 IDE and Ubuntu 18.04.5 LTS.

Related

How to run express server on Shared Hosting?

My shared hosting doesn't allow me customization. But, I anyhow installed nodejs. I also wrote an express server code inside /var/www/test-server directory.
const express = require('express')
const app = express()
const port = 8080
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
• But, I cannot access express server
• I can't use port 80.
• When I use some other ports like 8080, then I can see console output on Terminal as below:
example.work#example:/var/www/test-server$ node index.js
Example app listening on port 8080
Now, my question is —
"Is it possible to access express server with or without port through browser?"
Like: http://example.com/test-server
You must set up NGINX for the express server to be available on a Domain.
Alternatively, you can access the server at http://ipofthemachine:port
Make sure the port you are using is exposed if you have any firewall in place.

How to run nodejs on my public ip in windows?

I using windows 10 and want to run nodejs on port 3000 in my public address (just of me and for remote debugging).
So I open the port as say by this answer and I run this code from expressjs
const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => res.send('Hello World!'))
app.listen(port, () => console.log(`Example app listening on port ${port}!`))
Then I get myip address, and tell my friend to open http://256.266.266.266:3000/ in his chrome browser.
But the answer is This site can’t be reached ERR_CONNECTION_TIMED_OUT. why? how do I connect to my nodejs application in my public ip?
I do not have any antivirus or firewall as far as I know.
Telnet telnet 256.266.266.266 3000 say .Could not open connection to the host, on port...
netstat -an | find "3000" say TCP 0.0.0.0:3000 0.0.0.0:0 LISTENING

Openshift - port to use on deployment

I have the following start.js file:
var express = require('express');
var app = express();
app.use(express.static('static'));
var server = app.listen(8080, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});
In my NodeJs application on Openshift. However, when I run rhc tail-a app-name
I can see that there is an error of :
Error: listen EADDRINUSE :::8080
I've tried 80 and 443, and received those errors:
Error: listen EACCESS 0.0.0.0:443
Or 80
Which port should I use as default on my app?
Thanks!
Use Nginx,
Nginx (pronounced "engine x") is a web server. It can act as a reverse proxy server for HTTP, HTTPS, SMTP, POP3, and IMAP protocols, as well as a load balancer and an HTTP cache.
It isn't good practice to run your application with root privileges or directly run your application on port 80 and your port 8080 is in use. Try different port and use reverse proxy.
But if you want to run on port 80 or 443, run your application with root privileges.

Running multiple node express applications on single port

I am want to make proxy manger application for express,
but I have error all time that I try to do require for two diffrent applications.
i am using on app.use to route between the two application.
thanks alot
david
var app1 = require('./../app1/server/server');
var app2 = require('./../app2/server/server');
var app = require('express')();
app.use("/", app1);
app.use("/app2", app2);
app.listen(80, console.log("server up"))
Error is :
event,js:85 listen eaddrinuse
The port 80 is in use by another process. try with another empty port like 8080 or 3000. You can also try by stopping the application that is using port 80, then it will work.

Node.js + Express only work on port 3000 on test server

On Local Test it work. on different port (3001, 8080)
But on Test Server (Azure)
I run 2 instance of Node App on same machine
$ node api1/index.js (on port 3000)
$ node api2/index.js (on port 3001)
and
$ node api1/index.js (on port 3001)
$ node api2/index.js (on port 3000)
But it only works on port 3000.
How do I set different port in Express?
Now, I've changed at app.listen(3001) on index.js and it doesn't work.
Often cloud platforms set an environment variable that contains the port they want you to stick your app on. I don't have any experience with Azure... See the answer here: How to run a node.js server on Azure?
Specifically:
var port = process.env.port
Most cloud providers in my experience don't let you play on other ports. You can always specify a localhost port too, though by doing this:
var port = process.env.port || 3001 //(or whatever)
app.listen(port);
this way if process.env.port is undefined (which it will be in your dev environment) you fallback to 3001.
Make sense?

Resources