Tutorial 4: Node.js - common error 1: contentType and undesired downloading
Many of beginners may face problem where they want to show some content and the file gets downloaded (the browser does not show the content in its window). This problem arises because of not setting the content type of response.
It is advisable to set the content type, If no content type is defined browser try to detect the type of content. If it contain some buffer (or any type that browser is not designed to render) it tries to download the file.
Normally it does not happen as most of the time the file is in html (or other browser/plugin known format like images, videos, text, pdf).
A small file demonstrating use and effect of setting content type with node.js:
var http = require('http');
/*var server = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.end("\n");
});
var server = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "html"});
response.end("\n");
});
var server = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "application/octet-stream"});
response.end("\n");
});
*/
var server = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "text/html"});
var b = new Buffer (4); //4 for having a nice printed buffer, but the size will be 16KB
new Buffer ([0x00, 0x01, 0x02]).copy (b);
response.end('hi'+b);
});
/*
var server = http.createServer(function (request, response) {
var b = new Buffer (4); //4 for having a nice printed buffer, but the size will be 16KB
new Buffer ([0x00, 0x01, 0x02]).copy (b);
response.end(b);
});
*/
server.listen(8181);
console.log("Server running at http://127.0.0.1:8181/");
For a list of commonly used content types visit:
http://webdesign.about.com/od/multimedia/a/mime-types-by-content-type.htm
It is advisable to set the content type, If no content type is defined browser try to detect the type of content. If it contain some buffer (or any type that browser is not designed to render) it tries to download the file.
Normally it does not happen as most of the time the file is in html (or other browser/plugin known format like images, videos, text, pdf).
A small file demonstrating use and effect of setting content type with node.js:
var http = require('http');
/*var server = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.end("\n");
});
var server = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "html"});
response.end("\n");
});
var server = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "application/octet-stream"});
response.end("\n");
});
*/
var server = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "text/html"});
var b = new Buffer (4); //4 for having a nice printed buffer, but the size will be 16KB
new Buffer ([0x00, 0x01, 0x02]).copy (b);
response.end('hi'+b);
});
/*
var server = http.createServer(function (request, response) {
var b = new Buffer (4); //4 for having a nice printed buffer, but the size will be 16KB
new Buffer ([0x00, 0x01, 0x02]).copy (b);
response.end(b);
});
*/
server.listen(8181);
console.log("Server running at http://127.0.0.1:8181/");
For a list of commonly used content types visit:
http://webdesign.about.com/od/multimedia/a/mime-types-by-content-type.htm
Comments
Post a Comment