Nodejs服务器:加载资源失败:服务器的响应状态为404(未找到)

Nodejs server: Failed to load resource: the server responded with a status of 404 (Not Found)

本文关键字:服务器 响应 资源 加载 失败 Nodejs 状态      更新时间:2023-09-26

我在节点中设置了一个web服务器。

var app = express();
app.use(express.static(PUBLIC_PATH));
server = require('http').createServer(app),
io = require('socket.io').listen(server);

我将公用路径设置为我的应用程序的根公用文件夹,我将前端放在那里,所以当部署服务器时,它是/app/public

当我试图打开一个页面时,我在chrome的控制台中看到了这一点

i加载资源失败:服务器的响应状态为404(未找到)

有问题的文件是app.js,它位于我的公共文件夹中。我确实看到了来自网络服务器的对特定文件的请求

2015-04-04T15:43:04.208178+000:00 heroku〔路由器〕:at=info method=GETpath="/style.css"host=hive-badescuga.herokuapp.comrequest_id=2be03034e-e968-4397-8c2e-9c65c85fea22 fwd="188.25.247.211"dyno=web.1连接=1ms服务=6ms状态=304字节=236

2015-04-04T15:43:04.203415+000:00 heroku[路由器]:at=info method=GETpath="/app.js"host=hive-badescuga.herokuapp.comrequest_id=fc8c529c-7641-4df2-9b13-5fee9e1af365 fwd="188.25.247.211"dyno=web.1连接=1ms服务=7ms状态=404字节=217

2015-04-04T15:43:09.152566+000:00 heroku[路由器]:at=info method=GETpath="/style.css"host=hive-badescuga.herokuapp.comrequest_id=b89ded8e-5433-4b57-8c80-c2e7407d31b0 fwd="188.25.247.211"dyno=web.1连接=1ms服务=6ms状态=304字节=236

问题出在哪里?

更新

当我进一步调试时,我看到与我的app.js位于同一根目录下的css(无法加载)被加载。这是我的html:

<link rel="stylesheet" href="/style.css">
<script type='text/javascript' src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true&libraries=places"></script>
<script src="/app.js"></script>

所以style.css确实加载了,而app.js没有加载(因为找不到),而且它们在同一个文件夹中。

一切都已修复,app.js git被忽略,所以它在本地工作,但没有上传。

我不知道它是否会有帮助,但可能你应该写:process.env.PUBLIC_PATH。另外,我不知道你是否有这个:

var express = require('express');

我有这个:

app.use(express.static(path.join(__dirname ,'views')));