Consider the following example:
var http = require('http');
var server = http.createServer(function(request, response) {
response.writeHead({
'content-type': 'text/plain'
});
response.end('Hello world!');
});
server.listen(8000);
Why do I have to put the content-type property name into quotes? Isn’t writeHead expecting a plain JS object? Why can’t I write something like:
{
content-type: 'text/plain'
}
You must quote the property name of a JavaScript object literal if the name is not a valid identifier (i.e. something you could use as a variable name); integers are apparently ok too. Since the dash character (
-) is not a valid part of an identifier you must quote the string.