var http = require('http')
var moment = require('moment')
var url = require('url')

function stringifyTime(now) {
  var timeString = '{"hour":' + now.hour() +
                  ',"minute":' + now.minute() +
                  ',"second":' + now.second() + '}'
  return timeString;
}

function unixifyTime(now) {
  var timeString = '{"unixtime":' + now.valueOf() + '}';
  return timeString;
}

var server = http.createServer(function(req, resp) {
  if (req.method != 'GET') {
    resp.writeHead(405, {'Allow': 'GET'})
    resp.end('{"message": "bad method (method != GET)"}')
  }

  var hash = url.parse(req.url, true)
  var fcn = (function(now){ return null });

  switch(hash.pathname) {
    case '/api/parsetime':
      fcn = stringifyTime;
      break;
    case '/api/unixtime':
      fcn = unixifyTime;
      break;
  }

  var dateString = hash.query.iso
  var dateObj = moment(dateString)

  resp.writeHead(200, {'Content-Type': 'application/json'})
  resp.end(fcn(dateObj))
})

server.listen(8000)

var http = require('http')
var moment = require('moment')
var url = require('url')

function stringifyTime(time) {
  return {
    hour: time.hour(),
    minute: time.minute(),
    second: time.second()
  }
}

function unixifyTime(time) {
  return { unixtime: time.valueOf() }
}

var server = http.createServer(function(req, resp) {
  if (req.method != 'GET') {
    resp.writeHead(405, {'Allow': 'GET'})
    resp.end(JSON.stringify({message: 'bad method (method != GET)'}))
  }

  var parsed = url.parse(req.url, true)
  var result = null
  var date   = moment(parsed.query.iso)

  switch(parsed.pathname) {
    case '/api/parsetime':
      result = stringifyTime(date)
      break;
    case '/api/unixtime':
      result = unixifyTime(date)
      break;
  }

  resp.writeHead(200, {'Content-Type': 'application/json'})
  resp.end(JSON.stringify(result))
})

server.listen(8000)

Use JS Objects for generating JSON
Use JS Objects for generating JSON

Replace hand-coded JSON strings with JSON hashes (or Objects, I'm not
sure what they are defined as at this point.)
