我正在使用 Express、Node.js 和 socket.io 构建聊天应用。
我尝试将我的 css 文件 chat.css 链接到 html 文件 index.html 3 天但没有成功。
index.js
index.html
最佳答案
这就是发生的事情(高级解释)
- 浏览器请求加载 HTML 文件
index.html
。 - 然后,浏览器会执行一些任务,其中之一就是加载js、CSS等资源。
- HTML 有一个资源位于
assets/chat.css
,该路径名属于当前域,因此,浏览器发出请求,例如,http://域/ Assets /chat.css
。 - 服务器端接收该请求,但是,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/