Question

Help resolving 'site can't be reached' on express route with nodejs and passport js

I’ve been trying to deploy my web app the past few days, and while I can reach my site, when I try to navigate to an express route activated by a login button it gives me ‘localhost refused to connect’. My pages are served on localhost:3000, and my passport authentication and mongodb requests are made on port 4000.

Here is my /etc/nginx/sites-available/default

server {

        root /var/www/html;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

        server_name <my_url> www.<my_url>;

        location ^~ /assets/ {
                gzip_static on;
                expires 12h;
                add_header Cache-Control public;
        }

        location / {
                proxy_http_version 1.1;
                proxy_cache_bypass $http_upgrade;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;

                proxy_pass http://localhost:3000;
        }
        location /Notes {
                proxy_pass http://localhost:4000;
                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/<my_url>/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/<my_url>/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 = <my_url>) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


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

        server_name <my_url> www.<my_url>;
    return 404; # managed by Certbot


}

My routes are defined below, I’ve only included a few for brevity.

let PORT = 4000;

mongoose.connect('mongodb://localhost:27017/Notes', { dbName: "notes", useNewUrlParser: true>
const connection = mongoose.connection;

app.route('/Notes/auth/google').get( async (req, res, next) => {
    passport.authenticate('google', {scope: ['profile']})(req, res, next);
});

app.use(cors({credentials: true, origin: 'http://localhost:3000'}));
app.use("/",noteRoutes);
app.use('/Notes/loggedin', async (req, res) => {
    res.send(req.user);
});
app.listen(PORT, function(){
    console.log(`Server is running on port ${PORT}`);
    //console.log(app._router.stack);
});

My setup works great on localhost running on my own machine, but after deployment I cannot for the life of me get the redirect working. I am fairly certan I have the passport authentication setup correctly with the google API configured.

I’ve tried a few different configurations of the /etc/nginx/sites-available/default file, and haven’t had any luck, though I’m not sure I am understanding it properly. Port 4000 has been allowed with ufw.

Any help/suggestions would be extermely helpful, 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
November 13, 2022

Hi there,

You need to make sure that the service is actually listening on that port, you can use the following command:

netstat -plant | grep 4000

If you don’t get any output, this could indicate that the service is listening on a different port or is not running. What you need to do in this case is:

  • Find the port that your service is listening on, or if it is not running yet make sure to start it
  • Then adjust the port in your Nginx config file to match the port that the service is listening on

If you are using UFW already, make sure that port 80 and 443 are allowed as those are the ports that Nginx is listening on.

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

card icon
Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Sign up
card icon
Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We’d like to help.

Learn more
card icon
Become a contributor

You get paid; we donate to tech nonprofits.

Learn more
Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow – whether you’re running one virtual machine or ten thousand.

Learn more ->
DigitalOcean Cloud Control Panel
Get started for free

Enter your email to get $200 in credit for your first 60 days with DigitalOcean.

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

© 2023 DigitalOcean, LLC.