javascript - 我无法将 css 文件连接到 node.js 服务器上的 html 文件

标签 javascript html css node.js express

我正在使用 Express、Node.js 和 socket.io 构建聊天应用。

我尝试将我的 css 文件 chat.css 链接到 html 文件 index.html 3 天但没有成功。

index.js

index.html

最佳答案

这就是发生的事情(高级解释)

  1. 浏览器请求加载 HTML 文件 index.html
  2. 然后,浏览器会执行一些任务,其中之一就是加载js、CSS等资源。
  3. HTML 有一个资源位于assets/chat.css,该路径名属于当前域,因此,浏览器发出请求,例如,http://域/ Assets /chat.css
  4. 服务器端接收该请求,但是,Express 应用程序没有任何名为 /assets/chat.css 的已配置端点,您可能正在接收 HTTP 代码 404 未找到。

一个解决方案是将以下内容添加到 index.js 文件中

app.get('/assets/chat.css', function() {
  //set headers to the response.
  // Of course, you know where the chat.css file is stored, this is an example
  res.sendFile(parent_folder + '/public/css/chat.css');
});

关于javascript - 我无法将 css 文件连接到 node.js 服务器上的 html 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53354576/

相关文章:

html - 在网络浏览器上播放本地视频文件

python - 如何创建在缩放时动态调整标记大小的 html 绘图

javascript - 收到错误 Uncaught TypeError : Cannot set property 'value' of null

html - ie 的CSS 问题?

JavaScript - 结构体数组

javascript - 使用 PullToRefresh 插件

html - 带图像的按钮 VS html/css 按钮

javascript - Jquery 滚动条问题 - 调整窗口大小或打开 Firebug 解决

javascript - 下面的 javascript 片段有什么不同吗?

javascript - 使用 Flask 和 jquery 显示弹出窗口