Express JS route continues to execute endlessly - node.js

I am new to Express JS and Node in general, and I am having a slight problem here that I don't understand its reason.
So basically, this is my code for index.js:
const express = require('express')
const app = express();
app.get('/', (req, res) => {
console.log("Continuous message!");
res.send("Hello World");
});
app.listen(3000, () => console.log('Example app listening on port 3000!')
)
Everything works great except for one thing. In my console the message "Continuous message!" keeps showing up endlessly in my console. Am I doing anything wrong here? Or is this the normal behavior here?
Thank you!
Update: the reason turned out to be the redirection from port 80 to port 3000. So I edited server's site-available and the repetition no longer occurs. Question is now, how do I configure my AWS EC2 server to redirect traffic from port 80 to port 3000 without previous problem?
Update: I tried the EC2 Load Balancer and iptables commands. The problem is still there. Console messages and route code continues to execute every like 5 seconds. Which causes problems with my code flow.

Related

App.listen on an already running port of shared hosting

var express = require('express');
var app = express();
app.get("/", function(req,res){
console.log("running on that weird path i made///");
res.send("HELLO THIS IS A WEBPAGE TRYOUT!");
});
app.listen(3000, function(){
console.log("Server running on 3000");
})
I’m trying to create a simple app (for practice) which does a simple hello world. Using express via node.js
I can do it locally no problems.
The problem is I’m trying to run it on an existing server using cPanel shared hosting.
And there is obviously an already running domain name(and has website installed in Wordpress), so I cannot do a simple app.listen to a port which is obviously already running. And I’m guessing therefor I cannot do app.get(“/“....)
I’ve tried a different port-but the get request won’t work(I think obviously since that’s not the one running?)
So when I tell it to listen to the cPanel port, it throws an error that it’s already running.
And I don’t want to stop from the website to run. Is there a way to work this?
Tried also using subdomains. Same result.
Edit:
this worked on postman, when I've sent a get request to that address. tried port 3000, and it showed my res.send on the postman tab.
Edit2:
I've solved this...i forgot to put :3000 on the address..
as in www.example.com:3000
this doesn't work if i don't add the :3000.
and also if i do process.env.PORT and .IP..guessing its because its a shared hosting, or because wordpress is installed...
I won't delete just in case anyone did the same mistake as me.
If the problem that the port is already in use, you may try to use 0 as a port that will cause it to take a random free port on the machine.
const server = app.listen(0, function(){
console.log(`Server is listening on http://localhost:${server.address().port}`);
})

one nodejs/express app only works on localhost, but not using IP address, another nodejs/express app works just fine?

I have a super simple Hello World app that I got to run on myipaddress:3000 instead of just localhost:3000 by doing:
var express = require("express"),
app = express(),
bodyParser = require("body-parser"),
http = require("http");
app.use(bodyParser.urlencoded({ extended: true }));
app.get("/", function(req, res) {
res.render("home.ejs");
});
app.listen(3000, "0.0.0.0", function() {
console.log('Server running on port 3000');
});
but i have another, more involved node.js/express app that also uses mongoDB/mongoose where when I change the app.listen to include "0.0.0.0", it still only runs on localhost:3000. I'm not sure what could be causing this, maybe something to do with mongoDB? that's the only real difference between that apps that I can think of that would make a difference. I'm not running them both on port 3000 at the same time. I also tested the Hello World with a different port and it worked fine, but the other app did not.
Does anyone know what else could be the issue here? Let me know if you need to see any other code.
As I know 0.0.0.0 is given as a host to access from the outside interface. And If you don't specify the host while calling app.listen() The server will run on all available interface ie. on 0.0.0.0 but you can bind the IP address like
app.listen(3000, '127.0.0.1', function(){
console.log('Server running on port 3000');
});

Trying to proxy with Node and express-http-proxy and failing

I'm launching NPM express in a very simple app and wanting to take what is passed into the URL and redirect it as follows. Assuming I'm listening for web traffic on 8080 and I want to proxy my rest calls to port 5000.
That is, when the URL http://localhost:3077/rest/speakers comes in, I want the results to come from http://localhost:5000/rest/speakers (where the word speakers could be sessions, attendees or any other name like that.
app = express();
app.use('/rest', proxy('http://localhost:5000/rest'));
app.listen(8080, function () {
console.log('Listening on port 8080');
});
I seem to get the result of localhost:5000 but not even sure of that.
I changed to using http-proxy-middleware and this solved my problem.
app = express();
const proxy = require("http-proxy-middleware");
const targetVal = 'http://localhost:5000/rest';
app.use(proxy('/rest', {target: process.env.DEV_RESTURL, changeOrigin:true}));

learning node express, why console does not output 'localhost'?

I am learning to use express and node in general and I realise that I could be missing many fundamental knowledge. Below is the code for starting a server to serve static files, and as i understand the console should output the server address and port number on startup.
var express = require('express');
var app = express();
app.use(express.static('resources'));
app.use(express.static(__dirname));
app.get('/', function (req, res) {
res.sendFile( __dirname + "/" + "index.html" );
})
var server = app.listen(3000, function () {
var host = server.address().address
var port = server.address().port
console.log("Server listening at http://%s:%s", host, port)
})
I am expecting to see 'Server listening at http://localhost:3000"' but instead i get: 'Server listening at http://:::3000'
Can anyone explain what this means please
thanks
See the documentation on net.Server::address().
It probably doesn't say localhost because you didn't specify what address you were listening for.
By default, there is no specific address because it will accept incoming requests on the port regardless of the address it was sent to.
Examples of various addresses it could accept are localhost, 127.0.0.1, 192.168.1.72 or if you port-forward your server through your network, it could be your global IP address. If you explicitly specify an address, it will reject incoming requests from all these other addresses.
Sounds like the perils of asynchronous execution of code. You are defining the host and port and immediately calling them in the console log statement. NodeJS is executing the log statement before initializing. Try printing the log statement outside the app.listen function.

Node app on AWS returning 503

I have an Hello World node app:
var express = require('express');
var app = express();
app.get('/', function (req, res) {
console.log('in the get /');
res.send('Hello World!');
});
app.listen(8080, function () {
console.log('Example app listening on port 8080!');
});
I've pushed this to my EC2 instance. I go to my url and the page is black. I see a 503 is coming back. Im seeing live Node logs and the app is going into app.get because I see 'in the get /' repeatedly.
I have 2 instances. The first is running Nginx and requests to example.net get redirected to https://www.example.net. I also have a Load Balancer listening which take requests to www.example.net and directing them to my Node instance.
Incidentally, every few seconds I see a new 'in the get /' line. So my app is getting hit repeatedly from God knows where. Could this be comething to do with getting 503 (which indicates server is busy)? Note: this worked fine yesterday.
EDIT
The app suddenly started returning "Hello World". I then restarted the app - making no code changes - and im back getting 503's again
The ELB is detecting that your instance is unhealthy because it's down during a restart (Unhealthy Threshold). Then it has to pass the health check a certain number of times before it is healty again (Healthy Threshold). You can configure all this in the ELB settings.
I would decrease the Unhealthy Threshold if you only have one instance in your pool. And possibly decrease the HealthCheck Interval as well.

Resources