0

I am trying to run my node js program on port 8000, but it downloads the output instead of viewing on the browser.

var http = require('http'); 
function onRequest(request, response){ 
    response.writeHead(200, {'Content-Type' : 'plain/text'}); 
    response.write("hi");
    response.end(); 
} 
http.createServer(onRequest).listen(8000);

1 Answers1

0

This is occurring as you have you MIME types off. You are using plain/text, whereas the correct MIME type is text/plain. I've flagged to close this question due to it's typographical rather than logical error, but I will leave a note regarding what some others have noted in the comments of the question, just to provide a little tidbit on the matter for anyone who might stumble across this question.

Many of the commenters are correct in stating that some major browsers assume that the MIME type text/plain is meant for file downloads, as many servers are incorrectly configured to return text/plain for certain files. Some browsers, as have been noted, will attempt to "sniff" the data attempts to deduce if the correct MIME type is being used. This has been mainly seen in Chrome and Internet Explorer. Firefox saw it for quite some time in regards to links to stylesheets with incorrect MIME types, but it has since been removed for foreign URL's for security purposes. The simplest way around this is to add the X-Content-Type-Options header to the response, and give it a value of nosniff. This will help to prevent browsers from attempting to guess the MIME type, which can sometimes produce unintended results, albeit not in this instance.

Jeffrey
  • 1,271
  • 2
  • 15
  • 31