skip to Main Content

I’ve built a web app with an express backend and using the ejs view engine.

When I run it on my computer it works fine but I’m trying to host in on digitalocean. I cloned it to the droplet(Ubuntu 22.10 x64) and served it on port 80. When I visited it in the browser at 46.101.145.210:80, I got these errors.

GET https://46.101.145.210/javascripts/cookieHandler.js net::ERR_CONNECTION_REFUSED               
GET https://46.101.145.210/javascripts/domain.js net::ERR_CONNECTION_REFUSED

Here’s my file structure,

enter image description here

Here’s the code in index.js.

const express = require("express");
const app = express();
const fs = require("fs");
const path = require("path")
const assert = require("assert");

const PORT = 80

app.use(express.json());
app.use(express.urlencoded({extended: true}));
app.use(express.static('public'));

app.set("view engine", "ejs")

app.listen(PORT, () => {
    console.log("Server running on port " + PORT.toString());
});

const domains = ... // Not important

app.get("/", (req, res) => {
    res.render("index")
    //res.redirect("/domain")
})

app.get("/domain", (req, res) => {
    res.render("domain", {domains: domains})
})

I’ve tinkered with the firewall config and the express.static to see if it would make a difference. I couldn’t figure anything out though.

Update: I’ve checked the requests out a bit. They are https requests. When I use postman and change the request to be http it works.

2

Answers


  1. Chosen as BEST ANSWER

    I solved it by adding a valid ssl certificate.


  2. Looking your file structure and the code, I can say, you must define the full path of the public directory using

    express.static(__dirname+'/public')
    

    Or can be like

    const path = require('path');
    
    // .... your code ...
    
    app.use(express.static( path.join(__dirname, 'public') ));
    

    with this you are forcing to define where the public directory is on the ubuntu, and validate you are accessing your files

    Hope you can solve with this

    Update: checking carefully, I can see that your are trying to request using https, but your code is working on port 80 (http), so you said, on postman worked without https because of the port. If you want to have working your code with port 443 (https), you must have a certificate (with domain but not required), then try with https://46.101.145.210/javascripts/cookieHandler.js otherwhise try http://46.101.145.210/javascripts/cookieHandler.js.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search