4

I am currently trying to deploy a node js app on an AWS EC2 Windows Server 2019 instance. I am done with cloning my git repo and starting up my application and can access it within my EC2 instance.

To make my node application accessible I used the Internet Information Services (IIS) Manager and added the app under Default Web Site. I added the URL Rewrite module and added an inbound rule like so:

enter image description here

I can access the EC2 instance via its "Public DNS (IPv4)", which shows the default IIS Template: http://ec2-13-58-250-47.us-east-2.compute.amazonaws.com

But as soon as I add something to the path such as "/app", ":3000/app" or "/app:3000" I received "500" or "404" errors. What would be the correct path to access my app?

Other things I tried:

Security Groups

I googled a bit and most threads suggest checking the security group for the EC2 instance, which is what I have done. Here is a screenshot of my current security group:

Security Group

Port

My .env file does not contain a PORT, which is why I expect the app to use the hard-coded port 3000. To make sure i printed the port via listener.address().port and it uses 3000.

The app.js file looks like this:

const express = require("express")
const path = require("path")
const router = require("./router/auth.routes")
const bodyParser = require("body-parser")
const cookieParser = require("cookie-parser")
const passport = require("passport")
const winston = require('./config/winston')
const { initialiseAuthentication } = require("./auth")
const { connectToDatabase } = require("./database/connection")
require("dotenv").config()

/**
 * App Variables
 */
const app = express()
const port = process.env.PORT || 3000

/**
 * App Configuration
 */
app.use(compression())
app.set("views", path.join(__dirname, "views"))
app.set("view engine", "pug")                         
app.use(express.static(path.join(__dirname, "public")))
app.use(express.json())                                
app.use(bodyParser.urlencoded({ extended: true }))
app.use(cookieParser())
app.use(passport.initialize())                     
initialiseAuthentication(app)

/**
 * Routes Definition
 */
app.use('/', router)

async function establishConnection() {
  await connectToDatabase()
    .then(() => winston.log('info', 'Connection to database established.'))
    .catch(err => {
        winston.log('warn', 'Retrying')       
        setTimeout(establishConnection, 2000)
    })
}

/**
 * Server Activation
 */
app.listen(port, () => {
    establishConnection()
  })

Any suggestions? I am currently stuck here as this is the first time for me setting up an EC2 instance and with a nodejs app.

paaax
  • 144
  • 1
  • 1
  • 8

1 Answers1

0

Is the app listening to your "Public DNS (IPv4)"? In order for the app to be accessible, it has to be ready to listen to requests coming through your Public DNS (IPv4) through port 3000 or "http://ec2-13-58-250-47.us-east-2.compute.amazonaws.com:3000".

void
  • 1
  • I thought that's what the URL Rewrite module would be used for. I did it based on this tutorial: https://dev.to/massivebrains/deploying-node-express-app-on-a-windows-server-2l5c – paaax Apr 13 '20 at 08:51
  • based on the tutorial i do not need to change anything in the app itself. – paaax Apr 13 '20 at 09:01
  • The way I understand it is this: 1. Accessing my-URL shows the default IIS Template 2. Accessing my-URL/app shows 500 error 3. Adding a URL rewrite rule /app to "localhost:3000§ should resolve the error in 2 4. Accessing my-URL/app should now show the application, which is running on the EC2 instance. But it doesn't show up. – paaax Apr 13 '20 at 09:07
  • @paaax yep the app is not the problem. You need to setup a proxy on your windows server that redirects requests to your app that's running on http://localhost:3000 – void Apr 13 '20 at 09:08
  • that's why I am using IIS. I did it exactly as described in the tutorial – paaax Apr 13 '20 at 09:10
  • try this one https://dev.to/petereysermans/hosting-a-node-js-application-on-windows-with-iis-as-reverse-proxy-397b – void Apr 13 '20 at 09:17
  • it's the same approach. I read this one before. it's the same. this gives me a 404 error: http://ec2-13-58-250-47.us-east-2.compute.amazonaws.com/app:3000 – paaax Apr 13 '20 at 09:19
  • Okay, what exactly do you need? I tried experimenting with different settings. First attempt was to host the application under "Default Web Site" by choosing "Add application". Second attempt was to host the application by adding a new website – paaax Apr 13 '20 at 09:36
  • ``` ``` web.config of /app – paaax Apr 13 '20 at 09:49
  • Did anyone resolve this issue? – Juthi Sarker Aka Nov 22 '21 at 13:27