title: JSON+Node.js - Unexpected token o
body: <p>I just started working with node.js and json objects in my college course.  One of our assignments this week was to create a few json objects and extract parts of the object into an html page.  I thought I had a good grasp on how to do this, but I am running into an error when I try to start node.  If I remove the colleges object and the parse statement then node runs fine.</p>
  
  <p>Here is the error I get when I run "node index.js":</p>
  
  <pre><code>undefined:1
  [object Object],[object Object],[object Object],[object Object],[object Object
   ^
  SyntaxError: Unexpected token o
  at Object.parse (native)
  at Object.&lt;anonymous&gt; (/home/ubuntu/node_stuff/node_json/requestHandlers.js:13:20)
  at Module._compile (module.js:449:26)
  at Object.Module._extensions..js (module.js:467:10)
  at Module.load (module.js:356:32)
  at Function.Module._load (module.js:312:12)
  at Module.require (module.js:362:17)
  at require (module.js:378:17)
  at Object.&lt;anonymous&gt; (/home/ubuntu/node_stuff/node_json/index.js:3:23)
  at Module._compile (module.js:449:26)
  </code></pre>
  
  <p>And here is the code I am working with.</p>
  
  <pre><code>var querystring = require("querystring"),
  fs = require("fs"),
  formidable = require("formidable");
  
  var colleges = [
  {"name":"A-B Tech","street":"340 Victoria Road","city":"Asheville","state":"NC","zip":"28801","phone":"828-254-1921"},
  {"name":"UNC Asheville","street":"1 University Heights","city":"Asheville","state":"NC","zip":"28804","phone":"828-251-6600"},
  {"name":"UNC Charlotte","street":"9201 University City Blvd","city":"Charlotte","state":"NC","zip":"28223","phone":"704-687-8622"},
  {"name":"Western Carolina","street":"North Carolina 107","city":"Cullowhee","state":"NC","zip":"28723","phone":"877-928-4968"},
  {"name":"NC State","street":"2200 Hillsborough","city":"Raleigh","state":"NC","zip":"27695","phone":"919-515-2011"}
  ];
  
  var college = JSON.parse(colleges);
  
  function abtech(response) {
  console.log("Request handler 'abtech' was called.");
  
  var body = '&lt;html&gt;'+
  '&lt;head&gt;'+
  '&lt;meta http-equiv="Content-Type" '+
  'content="text/html; charset=UTF-8" /&gt;'+
  '&lt;/head&gt;'+
  '&lt;body&gt;'+
  '&lt;ul&gt;'+
  '&lt;li&gt;' + college[0].name + '&lt;/li&gt;'+
  '&lt;li&gt;' + college[0].street + '&lt;/li&gt;'+
  '&lt;li&gt;' + college[0].city + ' ' + college[0].state + ' ' + college[0].zip + '&lt;/li&gt;'+
  '&lt;li&gt;' + college[0].phone + '&lt;/li&gt;'+
  '&lt;/ul&gt;'+
  '&lt;/body&gt;'+
  '&lt;/html&gt;';
  
  response.writeHead(200, {"Content-Type": "text/html"});
  response.write(body);
  response.end();
  }
  
  function unca(response) {
  console.log("Request handler 'abtech' was called.");
  
  var body = '&lt;html&gt;'+
  '&lt;head&gt;'+
  '&lt;meta http-equiv="Content-Type" '+
  'content="text/html; charset=UTF-8" /&gt;'+
  '&lt;/head&gt;'+
  '&lt;body&gt;'+
  '&lt;ul&gt;'+
  '&lt;li&gt;&lt;/li&gt;'+
  '&lt;li&gt;&lt;/li&gt;'+
  '&lt;li&gt;&lt;/li&gt;'+
  '&lt;li&gt;&lt;/li&gt;'+
  '&lt;/ul&gt;'+
  '&lt;/body&gt;'+
  '&lt;/html&gt;';
  
  response.writeHead(200, {"Content-Type": "text/html"});
  response.write(body);
  response.end();
  }
  
  function home(response) {
  console.log("Request handler 'home' was called.");
  
  var body = '&lt;html&gt;'+
  '&lt;head&gt;'+
  '&lt;meta http-equiv="Content-Type" '+
  'content="text/html; charset=UTF-8" /&gt;'+
  '&lt;/head&gt;'+
  '&lt;body&gt;'+
  '&lt;h1&gt;Welcome to College&lt;/h2&gt;'+
  '&lt;p&gt;Where would you like to visit?&lt;/p&gt;'+
  '&lt;ul&gt;'+
  '&lt;li&gt;&lt;a href="/colleges"&gt;Colleges&lt;/a&gt;&lt;/li&gt;'+
  '&lt;li&gt;&lt;a href="/hours"&gt;Hours of Operation&lt;/a&gt;&lt;/li&gt;'+ 
  '&lt;li&gt;&lt;a href="/start"&gt;Upload a Photo&lt;/a&gt;&lt;/li&gt;'+
  '&lt;li&gt;&lt;a href="/show"&gt;View Gallery&lt;/a&gt;&lt;/li&gt;'+
  '&lt;/ul&gt;'+
  '&lt;/body&gt;'+
  '&lt;/html&gt;';
  
  response.writeHead(200, {"Content-Type": "text/html"});
  response.write(body);
  response.end();
  }
  
  function colleges(response) {
  console.log("Request handler 'colleges' was called.");
  
  var body = '&lt;html&gt;'+
  '&lt;head&gt;'+
  '&lt;meta http-equiv="Content-Type" '+
  'content="text/html; charset=UTF-8" /&gt;'+
  '&lt;/head&gt;'+
  '&lt;body&gt;'+
  '&lt;h1&gt;Colleges&lt;/h2&gt;'+
  '&lt;ul&gt;'+
  '&lt;li&gt;&lt;a href="/abtech"&gt;A-B Tech&lt;/a&gt;&lt;/li&gt;'+
  '&lt;li&gt;&lt;a href="/unca"&gt;UNC Asheville&lt;/a&gt;&lt;/li&gt;'+
  '&lt;li&gt;&lt;a href="/uncc"&gt;UNC Charlotte&lt;/a&gt;&lt;/li&gt;'+
  '&lt;li&gt;&lt;a href="/wcu"&gt;Western Carolina&lt;/a&gt;&lt;/li&gt;'+
  '&lt;li&gt;&lt;a href="/ncsu"&gt;NC State&lt;/a&gt;&lt;/li&gt;'+
  '&lt;/ul&gt;'+
  '&lt;/body&gt;'+
  '&lt;/html&gt;';
  
  response.writeHead(200, {"Content-Type": "text/html"});
  response.write(body);
  response.end();
  }
  
  function hours(response) {
  console.log("Request handler 'gallery' was called.");
  
  var body = '&lt;html&gt;'+
  '&lt;head&gt;'+
  '&lt;meta http-equiv="Content-Type" '+
  'content="text/html; charset=UTF-8" /&gt;'+
  '&lt;/head&gt;'+
  '&lt;body&gt;'+
  '&lt;h1&gt;Hours of Operation&lt;/h2&gt;'+
  '&lt;table&gt;'+
  '&lt;tr&gt;&lt;td&gt;Monday - Thursday&lt;/td&gt;&lt;td&gt;9 a.m. - 7 p.m.&lt;/td&gt;&lt;/tr&gt;'+
  '&lt;tr&gt;&lt;td&gt;Friday&lt;/td&gt;&lt;td&gt;9 a.m. - 5 p.m.&lt;/td&gt;&lt;/tr&gt;'+
  '&lt;tr&gt;&lt;td&gt;Saturday&lt;/td&gt;&lt;td&gt;9 a.m. - 12 p.m.&lt;/td&gt;&lt;/tr&gt;'+
  '&lt;/table&gt;'+
  '&lt;/body&gt;'+
  '&lt;/html&gt;';
  
  response.writeHead(200, {"Content-Type": "text/html"});
  response.write(body);
  response.end();
  }
  
  
  function start(response) {
  console.log("Request handler 'start' was called.");
  
  var body = '&lt;html&gt;'+
  '&lt;head&gt;'+
  '&lt;meta http-equiv="Content-Type" '+
  'content="text/html; charset=UTF-8" /&gt;'+
  '&lt;/head&gt;'+
  '&lt;body&gt;'+
  '&lt;h1&gt;Upload a file&lt;/h2&gt;'+
  '&lt;p&gt;It will be shown on the /show url after&lt;/p&gt;'+
  '&lt;form action="/upload" enctype="multipart/form-data" '+
  'method="post"&gt;'+
  '&lt;input type="file" name="upload" multiple="multiple"&gt;'+
  '&lt;input type="submit" value="Upload file" /&gt;'+
  '&lt;/form&gt;'+
  '&lt;/body&gt;'+
  '&lt;/html&gt;';
  
  response.writeHead(200, {"Content-Type": "text/html"});
  response.write(body);
  response.end();
  }
  
  function upload(response, request) {
  console.log("Request handler 'upload' was called.");
  
  var form = new formidable.IncomingForm();
  console.log("about to parse");
  form.parse(request, function(error, fields, files) {
  console.log("parsing done");
  
  /* Possible error on Windows systems:
     tried to rename to an already existing file */
  fs.rename(files.upload.path, "/home/ubuntu/node_stuff/node_assignment/test.jpg", function(err) {
    if (err) {
      fs.unlink("/home/ubuntu/node_stuff/node_assignment/test.jpg")
      fs.rename(files.upload.path, "/home/ubuntu/node_stuff/node_assignment/test.jpg");
    }
  });
  response.writeHead(200, {"Content-Type": "text/html"});
  response.write("received image:&lt;br/&gt;");
  response.write("&lt;img src='/show' /&gt;");
  response.end();
  });
  }
  
  function show(response) {
  console.log("Request handler 'show' was called.");
  fs.readFile("/home/ubuntu/node_stuff/node_assignment/test.jpg", "binary", function(error, file) {
  if(error) {
    response.writeHead(500, {"Content-Type": "text/plain"});
    response.write(error + "\n");
    response.end();
  } else {
    response.writeHead(200, {"Content-Type": "image/jpg"});
    response.write(file, "binary");
    response.end();
  }
  });
  }
  
  exports.start = start;
  exports.upload = upload;
  exports.show = show;
  exports.home = home;
  exports.colleges = colleges;
  exports.hours = hours;
  exports.abtech = abtech;
  </code></pre>
  
  <p>Any hints about what I am doing wrong would be very much appreciated.  My instructor is tough to get a hold of during the weekend, so I don't really have anywhere else to turn.  Thank you.</p>