如何解决NodeJS + Express不会加载公共目录,现在显示起始文件的js
这是添加完整项目之前我对服务器的测试代码:
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}/`);
});
问题1: JS代码显示您何时进入Page Site
问题2 :正在加载HTML页面,而不是src / images和stylesheet / index.css。
问题3 :基于前两个问题,该站点以前只能部分运行,现在除了加载脚本文件之外无法正常运行。
我使用Namecheap
作为主机。我习惯于前端开发,而不是后端。至于错误,我一直在图像和样式表中得到500错误代码。现在,我在控制台中没有收到错误,但是我的服务器确实收到了以下日志:
[ 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)
问题1 :代码看起来是否应该提取公共目录中的所有文件?
问题2:是什么导致网站停止加载index.html并仅显示JS?我没有触摸服务器的其余部分,只打开/关闭了代码和nodeJS服务器
解决方法
找出问题所在。必须更新HTML文件以正确包含公用文件夹(尽管它已经在公用文件夹中)。现在,图像和CSS已正确加载。我的服务器与本地服务器的工作方式有所不同,这就是为什么答案对我来说有点出乎意料的原因。谢谢大家的帮助。