basic javascript object and mime regarding a basic web-server code


i am new to programming with javascript. and i picked angular + node.js for server side. i went through the angular tutorial and i have a few question regarding the web-server.js they gave. as the server part is not covered in the angular tutorial i have some questions:

the server is ran with this command line:

node ./scripts/web-server.js

the main function that is being executed is "main" it is done via: main(process.argv);

and the function is:

function main(argv) {
  new HttpServer({
    'GET': createServlet(StaticServlet),
    'HEAD': createServlet(StaticServlet)
  }).start(Number(argv[2]) || DEFAULT_PORT);

function HttpServer(handlers) {
  this.handlers = handlers;
  this.server = http.createServer(this.handleRequest_.bind(this));

function createServlet(Class) {
  var servlet = new Class();
  return servlet.handleRequest.bind(servlet);

function StaticServlet() {}

StaticServlet.MimeMap = {
  'txt': 'text/plain',
  'html': 'text/html',
  'css': 'text/css',
  'xml': 'application/xml',
  'json': 'application/json',
  'js': 'application/javascript',
  'jpg': 'image/jpeg',
  'jpeg': 'image/jpeg',
  'gif': 'image/gif',
  'png': 'image/png',
  'svg': 'image/svg+xml'

my questions:

{    'GET': createServlet(StaticServlet),
    'HEAD': createServlet(StaticServlet)

i do not understand what these lines do. i assume this line creates an object, but the way that i know would be more like

    GET: createServlet(StaticServlet),
    HEAD: createServlet(StaticServlet)

what does the ' ' mean in those lines?

second question: what does these means?

.start(Number(argv[2]) || DEFAULT_PORT);

the .start functon is:

HttpServer.prototype.start = function(port) {
  this.port = port;
  util.puts('Http Server running at http://localhost:' + port + '/');

but i dont understand why is the || and argv[2] is actually null so why do we need to pass it.

third question: is regarding the creation of the StaticServlet.MimeMap. it is very similar to my first question i think. i do not understand the syntax here:

'txt': 'text/plain',.

what does it create?


Expanding on Jared's comments above.

Using the single quotes in both the HttpServlet constructor and the MimeMap initializer is likely just the original authors style preference.

There are times when this is necessary. For example when using a name that isn't a valid per the syntax rules or is a reserved javascript keyword.

From A Survey of the JavaScript Programming Language:

In the object literal notation, an object description is a set of comma-separated name/value pairs inside curly braces. The names can be identifiers or strings followed by a colon. Because of an error in the language definition, reserved words cannot be used in the identifier form, but they can be used in the string form.

For example:

    'my name here': 'bob',
    '0':            '',
    'new':          ''

The line:

.start(Number(argv[2]) || DEFAULT_PORT);

Basically just allows you to pass a port in on the command line so the caller can explicitly request that the server listens on that port without having to modify the source. For example:

node ./scripts/web-server.js 8080

And could be expressed as:

.start( Number(argv[2]) ? Number(argv[2]) : DEFAULT_PORT );

or the more lengthy:

var server = new HttpServer({…});
if(Number(argv[2]) {
} else {
By : dc5

This video can help you solving your question :)
By: admin