简体   繁体   中英

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.

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. I tried it with different browsers but result was same.


UPDATE
I could open html file in Edge. 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.
And tried giving another html file location, result was same.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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