0

If i have a httpserver in node.js that is serving a website on port 3000.

var httpserver = http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});

app.get('/', function(req, res) {
  res.render('index.html');
});

I need to access a txt file located at /public/

If i for example do localhost:3000/other_site.html, if this other_site.html is in /public/ it opens it, but if the file is txt it doesn't open it.

Is there a solution to this without having to install http-request, connect or other npm modules?

Regards,

Egidi
  • 1,736
  • 8
  • 43
  • 69

2 Answers2

1

Can you use express? If yes,

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

makes the trick.

If not, try searching the archives :) Using node.js as a simple web server

Community
  • 1
  • 1
LL1138
  • 126
  • 3
0

What error you are getting ?

if you experiencing browser error, try sending response header and set Content Type text/html and retry  

Example code

 fs.readFile('myfile.txt', function(error, file) {
        response.writeHead(200, {'content-type':'text/html'});
        response.end();
    });
anish
  • 6,884
  • 13
  • 74
  • 140