Question

Function timeout when trying to connect to managed Mongo
DB Database, Frontend is connected.

Hi! First time deploying to DigitalOcean and think I’m almost there. Can see either my backend or frontend. If I only run the server (https://outdoors.rest:8080/api/membership) I can see my membership JSON file with the following index.ts file.

import express from 'express'
import userRoutes from './routes/user-routes'
import membershipRoutes from './routes/membership-routes'
import memberPlanRoutes from './routes/plan-routes'
import passwordRoutes from './routes/password-routes'
import memberExtendRoutes from './routes/extend-routes'
import bodyParser from 'body-parser'
import mongoose from 'mongoose'
import dotenv from 'dotenv'
import cors from 'cors'
import path from 'path'

const app = express()

dotenv.config()

app.use(express.urlencoded({ limit: '50mb', extended: true }))
app.use(cors())
app.enable('trust proxy')

app.use((req, res, next) => {
  res.setHeader('Access-Control-Allow-Origin', '*')
  res.setHeader(
    'Access-Control-Allow-Headers',
    'Origin, X-Requested-With, Content-Type, Accept, Authorization'
  )
  res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PATCH, DELETE')
  next()
})

app.use('/api/membership/webhook', bodyParser.raw({ type: '*/*' }))
app.use(express.json({ limit: '50mb' }))
app.use('/api/user', userRoutes)
app.use('/api/membership', membershipRoutes)
app.use('/api/plan', memberPlanRoutes)
app.use('/api/password', passwordRoutes)
app.use('/api/extend', memberExtendRoutes)

mongoose.connect(process.env.MONGODB_URL as string).then(() => {
  console.log('connected to mongodb')
})

app.listen(8080, () => {
  console.log(`Your server is ready on port 8080!`)
})

If I include the following in my index.ts file

_// app.use(express.static(path.join(__dirname, 'static')));_
_// app.get('*', (req, res) => {_
_//   res.sendFile(path.resolve(__dirname, 'static', 'index.html'));_
_// });_

and run https:outdoors.rest, the frontend is connecting and running, but the backend isn’t connected, it times out. (membership info on home page)

console in dev tools says GET https://outdoors.rest:8080/api/membership net::ERR_CONNECTION_TIMED_OUT Uncaught (in promise)

My nginx setup:


        server {
 index index.html index.htm index.nginx-debian.html;

        server_name outdoors.rest www.outdoors.rest;
        location / {
                proxy_pass http://localhost:8080;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                proxy_set_header Host $host;
                proxy_cache_bypass $http_upgrade;
        }       
  listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/outdoors.rest/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/outdoors.rest/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot


}
server {
    if ($host = www.outdoors.rest) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = outdoors.rest) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


        listen 80 default_server;
        listen [::]:80 default_server;

        server_name outdoors.rest www.outdoors.rest;
    return 404; # managed by Certbot



}

Could someone please help and explain what I need to do in order to get the frontend connecting to the backend? Thank you!


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
July 18, 2023
Accepted Answer

Hi there,

The error that you are getting indicates that your firewall is not allowing access on port 8080 or that your express app listens on port 8080, but it doesn’t specify an IP address, so it should default to 0.0.0.0, eg:

```js
app.listen(8080, '0.0.0.0', () => {
  console.log(`Your server is ready on port 8080!`)
})
```

However, rather than opening port 8080 via your firewall and changing your bind address to 0.0.0.0, you can use Nginx to proxy requests to http://localhost:8080.

Here’s an example of how you can modify your nginx configuration to serve as a reverse proxy for your API as well:

server {
    server_name outdoors.rest www.outdoors.rest;
    location / {
            proxy_pass http://localhost:8080;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
    }       

    location /api {
            proxy_pass http://localhost:8080/api;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
    }

    # Other SSL configurations remain the same...
}

After you update your nginx configuration, don’t forget to reload or restart the nginx server to apply the changes.

This way, you can access your API via the same domain and protocol as your frontend, like so: https://outdoors.rest/api/membership and you will not have to specify the port as Nginx will take care of the proxying to the backend service.

Let me know how it goes!

Best,

Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.