Node.js NodeJS Express应用程序中未加载具有相对路径的本地文件

Node.js NodeJS Express应用程序中未加载具有相对路径的本地文件,node.js,express,static-files,Node.js,Express,Static Files,我正在尝试在Express和Node.js中构建一个应用程序。我的服务器文件包含在app文件夹中,前端文件位于公用文件夹中 我的文件结构如下: my_project |- app | |-(server stuff) | |- .... |- public | |- modules | | |- core | | | |- index.html | | | |- style | | |

我正在尝试在Express和Node.js中构建一个应用程序。我的服务器文件包含在
app
文件夹中,前端文件位于公用文件夹中

我的文件结构如下:

my_project
|- app
|     |-(server stuff)
|     |- ....
|- public
|     |- modules
|     |     |- core
|     |     |     |- index.html
|     |     |     |- style
|     |     |     |     |style.css
|     |     |     |     |sidebar.css
|     |     |     |- img
|     |     |     |     |- faceicon.png
|     |     |     |     |- main.png
|     |     |     |- js
|     |     |     |     |main.js
|     |     |     |     |sidebar.js
|     |     |- articles
|     |     |     |- ....
问题是在我的
index.html
文件中,当我引用类似

<link href="style/style.css" rel="stylesheet">
预期结果不会显示在屏幕上,我会收到如下控制台消息

GET http://localhost:3000/img/faceicon.png 500 (Internal Server Error)
如何解决此问题?

中间件基于Express应用程序,并负责为其静态资产提供服务

工作原理:

  • 从应用程序目录中的“public”目录为应用程序提供静态内容

    //获取/style.css等

    app.use(express.static(uu dirname+/public))

  • 在“/static”处装载中间件,以仅在其请求路径前缀为“/static”时提供静态内容

    //GET/static/style.css等

    app.use('/static',express.static(uu dirname+'/public')

  • 提供来自多个目录的静态文件,但优先于“/public”

    app.use(express.static(uu dirname+/public))

    app.use(express.static(uu dirname+/files))

    app.use(express.static(uu dirname+/uploads))


似乎您没有
css
文件夹。@AleksandrM抱歉,修复了图像路径应为
http://localhost:3000/modules/core/img/faceicon.png
@Sami但是如果我要更改服务器的端口呢?这个链接将不再有效,对吗?@Sami,但我不应该这样做
index.html
img
位于同一目录中。理论上,它应该与img/faceicon.png一起使用
GET http://localhost:3000/img/faceicon.png 500 (Internal Server Error)