Search code examples
javascriptnode.jsexpressnamecheap

NodeJS + Express wouldn't load public directory and now show the js of the starting file


This is my test code for the server before I add my full project:

const http = require('http').createServer();
var express = require('express');
var app = express();
var path = require('path');
const hostname = '127.0.0.1';
const port = 3000;

app.use(express.static('public'));
app.get('/', function(req, res) {
    res.sendFile(path.join(__dirname + '/public/index.html'));
});

app.listen(port, hostname, () => {
        console.log(`Server running at http://${hostname}:${port}/`);
});

Issue 1: The JS code shows when you go to Page Site

Issue 2: The HTML page was loading, but not the src/images and stylesheet/index.css.

Issue 3: based on the first two issues, the site used to work partially and now doesn't work besides loading the script file.

I'm using Namecheap as my host. I'm used to front-end development, but not back-end. As for errors, I kept getting a 500 error code for the images and stylesheet. Now, I don't get errors in the console, but my server does get a log with:

[ N 2020-08-10 16:04:54.2511 932457/T17 age/Cor/CoreMain.cpp:1117 ]: Checking whether to disconnect long-running connections for process 984629, application /home/username/matcher.withaliquid.com (production)

Question 1: does the code look like it should be pulling all the files in the public directory?

Question 2: what would've caused the site to stop loading the index.html and just show the JS? I didn't touch the rest of the server, only the code and the nodeJS server on/off


Solution

  • Figured out the issue. Had to update the HTML file to properly include the public folder (though it was already in the public folder). Now the images and css load properly. My server works a bit differently than locally, which is why the answer was a bit unexpected for me. Thanks guys for your help.