0

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;
  this.server.listen(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?

lobengula3rd
  • 1,821
  • 4
  • 26
  • 39
  • `''` (single or double) quotes simply denote a string; it's probable that around the `GET` and `HEAD` text they are unnecessary, if Node.js would read those as Javascript object notation to initializer, rather than JSON (this seems logical but I'm not a Node.js developer, per se). It will work either way, I would think. The `condition || condition` simply allows you to say `this OR that`, if the former is `falsey`. This allows for "default" settings (think of it as a shorthand for a simple `if`), here it's passed directly to the function, instead of storing first in a variable. – Jared Farrish Jul 28 '13 at 15:38
  • To be quite honest you should learn plain Javascript before you start picking up frameworks. These are some of the quirks and notations you would have picked up by now if you did. – CBIII Jul 28 '13 at 15:45
  • Be careful not to confuse *object notation* with JSON, the latter being a string that evaluates as *object notation*. It requires things like quoted key names (e.g., `'{"name":"value"}'`), whereas plain object notation does not (e.g., `{name:"value"}`). It depends on whether it is run as Javascript or *evaluated* as Javascript (hence JSON is a string, e.g., `eval('{"name":"value"}')`). – Jared Farrish Jul 28 '13 at 15:46
  • A [link](http://stackoverflow.com/questions/2904131/what-is-the-difference-between-json-and-object-literal-notation) to explain the differences. – CBIII Jul 28 '13 at 15:49

1 Answers1

1

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]) {
    server.start(Number(argv[2]);
} else {
    server.start(DEFAULT_PORT);
}
dc5
  • 12,341
  • 2
  • 35
  • 47