简体   繁体   English

浏览器下载html文件而不是打开

[英]Browser downloads the html file instead of opening

I have created a server with Node.JS express where I open html file in public folder. 我用Node.JS express创建了一个服务器,在公共文件夹中打开html文件。

app.use(express.static(__dirname + '/public'));
app.listen(8080);

I have done this before without any problems. 我之前没有遇到任何问题。 But in this project when I try to open the server in 127.0.0.1:8080 it automatically downloads the index.html file. 但是在这个项目中,当我尝试在127.0.0.1:8080打开服务器时,它会自动下载index.html文件。 I tried it with different browsers but result was same. 我尝试了不同的浏览器但结果是一样的。


UPDATE UPDATE
I could open html file in Edge. 我可以在Edge中打开html文件。 But it was very very slow like it is processing some thing. 但它处理某些事情非常非常缓慢。 And it got stuck when I send a request to the server. 当我向服务器发送请求时它就卡住了。
I tried opening the HTML file separately with the browser it works without any problem. 我尝试使用浏览器单独打开HTML文件,没有任何问题。
And tried giving another html file location, result was same. 并尝试给另一个html文件位置,结果是一样的。

I do not know what is the exact problem here. 我不知道这里的确切问题是什么。 But I got to know that it has to do something with content type as td-lambda mentioned in the comments. 但我知道它必须做一些内容类型,如评论中提到的td-lambda So I found a solution like this. 所以我找到了这样的解决方案。

var express = require('express');
var app = express();
var server = app.listen(8080);
app.set({
     'Content-Type': 'text/html'
});
app.use(express.static(__dirname + '/public'));

And this solved my problem. 这解决了我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM