Should I include res.writeHead in node.js when I do HTML5?

I started learning HTML5 / CSS3 / JavaScript / Node.js and had no experience with HTML4, not to mention web applications. So this may be a stupid question, but in any case, I could not see the answer when searching on the Internet, since all the examples that I read, both on the Internet and in books, were considered written in HTML4, here.

In Node.js, when you send a response header to a client, you should write the following code example:

var http = require('http'); http.createServer(function(req, res){ res.writeHead(200, {'Content-Type':'text/html'}); res.write(some_data); res.end(); }); 

However, in HTML5 you don’t need to write the header {'Content-Type':'text/html'} , right? Can I delete the res.writeHead line in the node application file? I know that some browsers still do not support HTML5, but if possible, I want to make my code suitable only for HTML5 / CSS3.

Also, when I finish creating a web application and publish it, which way should I go when I deal with a browser that still does not support HTML5 / CSS3 (let's say old IE).

1) Publish it anyway, even if some content there crashes and looks bad.

2) Completely block users who use the browser because of the security perspective, and suggest that they use a different browser if it is feasible (I do not know, but I suspect that I can access the information of the client’s browser and the deal with him, respectively, with using javascript ... right?)

I don’t want my code to be suitable for all browsers, since I want to publish it as a hobby and create a beautiful website using HTML5 / CSS3, spending as little time as possible.

Thanks.

+4
source share
2 answers

This requires browsers to recognize the page as HTML. content-type not part of the HTML5 (or HTML4) specification; This is part of the HTTP headers. this is what tells the browser which content it receives.

If you specify the wrong type of content, the browser most likely will not be able to display your page or simply display the content that it receives as the source text. The content type is a required part of the HTTP protocol.

If you need proof, here is the GitHub page, which is HTML code, but which is served with non-HTML Content-Type . As you can see, the browser displays it as plain text.

Some web servers may send the correct mime type for default HTML pages, depending on the server configuration (often it depends on the server that looks at the file name extension to determine which type of content to send, but this will depend on the server configuration )

But if the server is not configured to send the correct mime type for your page, then yes, you do have to do it yourself in your code. Browsers are not smart enough to handle it themselves.

You also need to provide the appropriate content-type headers for your CSS, JS, JSON, image files, etc. Again, if they serve as static files with recognized file name extensions, then the likelihood that the default server config will send the correct type of content without thinking about it. But if you are writing a program that creates these files on the fly, you will most likely need to correctly set the content type as part of your program so that the browser understands them and displays them correctly.

+5
source

However, in HTML5 you don’t need to write the header {'Content-Type': 'text / html'}, right?

Wrong. Each HTTP response that returns some content should indicate the Content-Type of the content.

+1
source

Source: https://habr.com/ru/post/1485223/


All Articles