【问题标题】:i can't connect css file to html file at node.js server我无法将 css 文件连接到 node.js 服务器上的 html 文件
【发布时间】:2018-11-18 20:40:58
【问题描述】:

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

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

index.js

index.html

【问题讨论】:

  • index.js 文件中的app.get('/assets/chat.css', function() {....}); 语句在哪里?
  • 寻求帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定的问题或错误以及重现它所需的最短代码 在问题本身。请参阅:如何创建minimal reproducible example
  • 请注意,“code-as-images”不够好,如果有任何不清楚的地方,请第二次查看How to Ask
  • 来自您服务器的每个资源都应配置为后端的端点。
  • Ele 没看懂你的回答,能详细点吗?

标签: javascript html css node.js express


【解决方案1】:

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

  1. 浏览器发出加载HTML文件index.html的请求。
  2. 然后,浏览器会执行一些任务,其中之一就是加载js、CSS等资源。
  3. HTML 中有一个资源位于assets/chat.css,该路径名属于当前域,因此,浏览器发出请求,例如,http://domain/assets/chat.css
  4. 服务器端接收到该请求,但是,快速应用程序没有任何名为 /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');
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-17
    • 2021-09-13
    • 2023-02-24
    • 1970-01-01
    • 2020-11-01
    • 1970-01-01
    • 2019-03-06
    • 1970-01-01
    相关资源
    最近更新 更多