从Node.js渲染HTML文件时,MIME类型的问题
我正在使用Node.js Web服务器。我也是socket.io和webrtc,可以在我的应用程序中获取音频流。我正在使用此处所示的路径 /音频从此处渲染HTML文件。
const webServer = http.createServer(app);
const socketIo = require("socket.io")(webServer); // web socket external module
app.use( '/assets', express.static( path.join( __dirname, 'assets' ) ) );
// Rendering html file
app.get( '/audio', ( req, res ) => {
res.sendFile( __dirname + '/index.html' );
} );
HTML文件包含CSS和JS文件,我将其加载如下。
<link rel="stylesheet" href='assets/css/app.css' type="text/css">
<script type="module" src='./assets/js/rtc.js'></script>
<script type="module" src='./assets/js/events.js'></script>
<script type="module" src='./assets/js/autolink.js'></script>
当我通过 /音频运行此HTML文件时,我无法加载这些CSS和JS文件。在控制台中,我会遇到此MIME类型错误。
我尝试了通过网络获得的所有解决方案。但是到目前为止,什么都没有。
I am using node.js webserver. I am also socket.io and webrtc for getting audio streaming in my application. I am rendering a html file from here using a path /audio as shown here.
const webServer = http.createServer(app);
const socketIo = require("socket.io")(webServer); // web socket external module
app.use( '/assets', express.static( path.join( __dirname, 'assets' ) ) );
// Rendering html file
app.get( '/audio', ( req, res ) => {
res.sendFile( __dirname + '/index.html' );
} );
The html file contains CSS and js files an I am loading them as below.
<link rel="stylesheet" href='assets/css/app.css' type="text/css">
<script type="module" src='./assets/js/rtc.js'></script>
<script type="module" src='./assets/js/events.js'></script>
<script type="module" src='./assets/js/autolink.js'></script>
When I am running this html file through /audio, I am unable to load these CSS and JS files. In the console I am getting this mime type error.
I have tried all the solutions that I have got through web. But nothing worked as of now.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试将标题ContentType设置为您的每个CSS和JavaScript文件的响应中,如下所示
。 ///对于每个CSS文件
res.setheader('content-type','application/javaScript'); //用于JS文件
Try to setting the header contentType in the response for every your css and javascript files like this below
res.setHeader('Content-Type', 'text/css'); /// for every css files
res.setHeader('Content-Type', 'application/javascript'); // for js files