简体   繁体   English

如何使用 Express.js 成功加载 Static 网站,无法获取 /index.html,错误

[英]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.我真的不知道我在做什么,但我有一个简单的 static 网站,我试图在我购买的 VPS 上托管。

My website only has index.html and css files along with some pictures.我的网站只有 index.html 和 css 文件以及一些图片。

I try to deploy this website in 8080 port by using Node.js and Express.js.我尝试使用 Node.js 和 Express.js 在 8080 端口部署这个网站。

I referred to this document here: https://www.freecodecamp.org/news/develop-deploy-first-fullstack-web-app/#creatingyourfirstserver我在这里参考了这个文档: 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".我安装了 node.js 和 express.js 并在名为“public”的文件夹中创建了 server.js。 This folder is also where my website files are located.此文件夹也是我的网站文件所在的位置。 "index.html" is located here. “index.html”位于此处。

I successfully selected the entry point to be server.js.我成功地选择了入口点 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然后在 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然后 go 到浏览器并输入 http://myVpsIP:8080/index.html

it says "Cannot GET /index.html".它说“无法获取 /index.html”。

Thanks.谢谢。

You told your server to look in the public folder for static files, like html, css, and browser-side js.您告诉您的服务器在public文件夹中查找 static 文件,例如 html、css 和浏览器端 js。 You used this line to do that.你用这条线来做到这一点。

app.use(express.static('public'));

Your FCC tutorial says this:你的 FCC 教程是这样说的:

First, move all your website's static files (HTML, CSS, JS, images, etc.) into a folder called public...首先,将您网站的所有 static 文件(HTML、CSS、JS、图像等)移动到名为 public...

So do that: create a folder (subdirectory) called public , and move index.html, index.js, styles.css, and, I guess, your Pictures folder into it.这样做:创建一个名为public的文件夹(子目录),然后将 index.html、index.js、styles.css 移动到您的图片文件夹中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM