Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I have to include res.writeHead in node.js when I deal with HTML5?

I've started to learn HTML5/CSS3/JavaScript/Node.js and have had no experience with HTML4, let alone web applications. So this may be a stupid question, but anyway I wasn't able to see an answer while looking for on the Web, since all examples I read, both on the Web and books, were assumed to be written in HTML4, so I'm here now.

In Node.js, when you send a response header to a client, you would write a following sample code:

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 a header {'Content-Type':'text/html'}, right? So can I remove a line res.writeHead in my node application file? I know some browsers still don't support HTML5, but if it's feasible, then I want to make my code suitable for only HTML5/CSS3.

Also, when I finish creating a web application and publish it, which path should I go to when coping with a browser which still doesn't support HTML5/CSS3 (say, old IE).

1) Still publish it even if some contents there collapse and are looking badly

2) Completely block users who use the browser due to the perspective of security and prompt them to use another browser, if it's feasible (I don't know, but I suspect I can get access to the client's browser information and deal with it accordingly using JavaScript...right?)

I don't want to bother to make my code suitable for all browsers, since I want to publish it as a hobby and build a good-looking web site using HTML5/CSS3 by spending as little time as possible.

Thanks.

like image 979
Blaszard Avatar asked Feb 16 '23 07:02

Blaszard


1 Answers

The content-type:text/html is required for browsers to recognise a page as HTML. The content-type is not part of the HTML5 (or HTML4) spec; it's part of the HTTP headers; it is what tells the browser what kind of content is is receiving.

If you provide the wrong content type, the browser is likely to be unable to display your page, or may simply display the content it receives as raw text. The content type is a mandatory part of the HTTP protocol.

If you need proof, here's a page on GitHub, which is HTML code, but which is served with a 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 HTML pages by default, depending on the server config (often this is based on the server looking at the file name extension to determine what content type to send, but this will depend on the server config).

But if the server isn't configured to send the correct mime type for your page, then yes, you do need to do it yourself in your code. Browsers are not clever enough to work it out for themselves.

You'll also need to provide appropriate content-type headers for your CSS, JS, JSON, image files, etc. Again, if these are being served as static files with recognised filename extensions then the odds are that the default server config will send the correct content type without you needing to think about it. But if you're writing a program that creates these files on the fly, then you will very likely need to set the content type correctly as part of your program in order for the browser to understand them and display them correctly.

like image 128
Spudley Avatar answered Mar 05 '23 17:03

Spudley