lundi 19 avril 2021

How to Successfully Load a Static Website with Express.js, Cannot GET /index.html, error

I really don't know what I am doing but I have a simple static website that I am trying to host on a VPS I bought.

My website only has index.html and css files along with some pictures.

I try to deploy this website in 8080 port by using Node.js and Express.js.

I referred to this document here: https://www.freecodecamp.org/news/develop-deploy-first-fullstack-web-app/#creatingyourfirstserver

I installed node.js and express.js and created server.js in a folder which is called "public". This folder is also where my website files are located. "index.html" is located here.

I successfully selected the entry point to be server.js.

$ ls -l
total 72
drwxr-xr-x  3 debian debian  4096 Apr 18 02:55 Concept
-rw-r--r--  1 debian debian  5081 Apr 18 02:55 index.html
-rwxr-xr-x  1 debian debian   189 Apr 19 11:52 index.js
drwxr-xr-x 52 debian debian  4096 Apr 19 11:43 node_modules
-rw-r--r--  1 debian debian   284 Apr 19 11:43 package.json
-rw-r--r--  1 debian debian 31240 Apr 19 11:51 package-lock.json
drwxr-xr-x  4 debian debian  4096 Apr 18 02:55 pictures
-rwxr-xr-x  1 debian debian   202 Apr 18 02:59 server.js
-rw-r--r--  1 debian debian  4621 Apr 18 02:55 styles.css

Then inside server.js

$ cat server.js
// Load Node modules
var express = require('express');
// Initialize Express
var app = express();
// Render static files
app.use(express.static('public'));
// Port website will run on
app.listen(8080);

Then when I do this:

$ npm start

> public@1.0.0 start /home/debian/public
> node server.js

and then go to browser and type http://myVpsIP:8080/index.html

it says "Cannot GET /index.html".

Thanks.




Aucun commentaire:

Enregistrer un commentaire