Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 245
Lasitha Yapa Avatar asked Jan 26 '26 16:01

Lasitha Yapa


1 Answers

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.

like image 63
Lasitha Yapa Avatar answered Jan 29 '26 06:01

Lasitha Yapa