Unable to run Nodejs app on AWS Beanstalk - node.js

I have deployed NodeJS app on AWS Beanstalk. I am using AWS code pipeline for deployment from Github. After deployment when I am visiting URL, it's showing below error on web page:
502 Bad Gateway
nginx/1.18.0
Below is my basic node code:
const express = require('express');
const app = express();
const port = 3000 || process.env.PORT;
app.get('/',(req,res) => {
res.send("Hello there");
});
app.listen(port,(req,res) => {
console.log(`App is running at ${port}`);
});
In below screen health of my environment is also showing severe. How can I resolve this?

Your load balancer needs a part of your application to "ping" which is sometimes called a heart beat or just a 'health check'.
This is usually just something that returns a HTTP 200 response to let the load balancer know that the host is healthy and can receive traffic.
I'm not a node.js guy but if you can create some endpoints like /health-check and just return an "ok" or HTTP 200 that will satisfy the need.
You can also have your health-check do something like check if the database connection is running as well. It's up to your specific use case.
Within beanstalk you would then designate /health-check as the URL endpoint for the load balancer to check.
Check out this link as well:
https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/using-features.healthstatus.html

This is the logic error => const port = 3000 || process.env.PORT;
Try with this assignment => const port = process.env.PORT || 3000;

Related

"Mixed content blocked" ;page was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint with https

I am using vercel to host my app and gcp to host my api
After a day of torturing, I notice that the mixed content is about my api being http, instead of https, which they also refer it as ssl, which I am not so familiar with it too.
So, luckily, the api is controlled by me and I allowed https traffic on my gcp setting already.
Then, I try it on Postman with https prefix, But I still get this saying error in ssl
I wonder if it is to do with my API code setting or there are still sth I was missing
My Node js api code, port is 8080 already. And testing is only work with http still.
const app = require("./app");
require("dotenv").config();
const port = process.env.PORT || 8080;
app.listen(port, () => {
console.log(`running on port ${port}`);
});

Accessing Express server on EC2

I'm using Express on my Amazon EC2 server. My server looks like this:
var express = require('express');
var app = express();
app.get('/my_view/true', function (req, res) {
//do something
res.render('view', {var1: somevalue});
});
app.listen(3000);
When I access my Express app locally using http://localhost/view/true, it works and I get my template displayed in browser.
But when I try use it on EC2 via ec2-myinstance.us-east-2.compute.amazonaws.com/view/true I get This site can’t be reached. refused to connect error.
I've added HTTP 80 port in my AWS EC2 Security Group settings but it's still not working.
Can someone help?
You should set the server to listen at port 80, instead you are listening at port 3000. So either open up the port 3000 in the security group or listen at the port 80.
app.listen(80);
The above change should work. Let me know if it works.

How to serve a nodeJS app via HTTPS on AWS EC2?

I'm trying to run a hello world express app on an EC2 instance and serve it via HTTPS.
Here is the server code:
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.send('Hello World!\n');
});
const server = app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});
server.keepAliveTimeout = 65000; // Ensure all inactive connections are terminated by the ALB, by setting this a few seconds higher than the ALB idle timeout
server.headersTimeout = 66000; // Ensure the headersTimeout is set higher than the keepAliveTimeout due to this nodejs regression bug: https://github.com/nodejs/node/issues/27363
I created an EC2 instance and let it run there. Additionally to get HTTPS, I fired up an Application Load Balancer with an SSL certificate. I created a listener on port 443 and forwarded it to port 3000 on my EC2. Lastly I set up a Route53 entry to point to that ALB.
All I get 24/7 is 502 Bad Gateway. Am I missing something basic here?
How to run the most basic express server via HTTPS?
For anyone who might stumble upon this some time later:
If you wish to terminate HTTPS on the load balancer and speak HTTP to your app behind it you need to select HTTP as prototoll and the port of your node app when creating a target group in the console.
For some reason I thought for hours this should be HTTPS and 443 when I want to accept HTTPS traffic.

404 - from nginx application to remote servers localhost?

I am new to react, and wanted to deploy a site to my domain with Nginx. I need to make the application to be able to fetch from client side, to the localhost of the remote server hosting the site with Nginx. I know exposing this many details might make security experts and hackers either drool or shake their heads. But I am losing my sanity from this.
This is a filtered version of my Node.js express service running on the remote server:
const express = require("express")
const cors = require("cors")
const app = express();
const PORT = 1234;
const spawn = require("child_process").spawn;
app.use(cors())
app.listen(PORT, function(){
console.log(`listening on port:${PORT}...`)
})
app.get("/api/play/:choice", function(req,res){
pythonProcess = spawn('python',["./script.py", req.params.choice]);
pythonProcess.stdout.on('data', function(data) {
res.status(200).send(data.toString('utf-8'))})
})
this is how I am fetching from the deployed react application. The public IP of the droplet I am using
fetch(`104.248.28.88/1234/api/play/rock`)
Change the fetch to replace the / with a : to indicate port, rather than directory
fetch("104.248.28.88:1234/api/play/rock")

Custom Port not working for node.js app on AWS EC2 instance

I have deployed the following code on my AWS EC2 instance -
const express = require('express')
const app = express()
app.get('/test',(req,res) => {res.send('Hi')})
app.listen(3001, () => console.log('Server running on port 80'))
When I try to visit the following url - http://ec2-13-59-209-0.us-east-2.compute.amazonaws.com/test , I get connection refused message. The message on the UI is ec2-13-59-209-0.us-east-2.compute.amazonaws.com refused to connect.
I did go through the documentation and set up security group to listen on port 3001. But that did not help either.So I enabled on traffic for all the ports. Still I was not able to connect. Please find below snapshot of the security group. It would be great if you can help me with this.
You need to tell Express to listen to all traffic, not just localhost traffic. Change your app.listen line to:
app.listen(3001, "0.0.0.0");

Resources