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);
8
  • what's the mime type when you are serving the page Commented Dec 28, 2017 at 5:18
  • Possible duplicate of Download a file from NodeJS Server using Express Commented Dec 28, 2017 at 5:20
  • post your full code Commented Dec 28, 2017 at 5:20
  • type is plain/text Commented Dec 28, 2017 at 5:20
  • This is my full code, Sorry there is an issue with alignment in this textbox...var http = require('http'); function onRequest(request, response){ response.writeHead(200, {'Content-Type' : 'plain/text'}); response.write("hi"); } http.createServer(onRequest).listen(8000); Commented Dec 28, 2017 at 5:22

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Yes, Correct MIME type is text/plain, but still my problem is not solved, Because output is not displaying on my browser, so question is not closed..
What browser are you using? Please include the version. @TusharWaghmode
I am using Google chrome browser and having version 63.0.3239.108 (64-bit)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.