Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Jan 2013
    Posts
    22
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Help with JSON and/or Node

    Note1: Not sure where else to put this, if it is moved by mods, may I be kindly notified of its new location?

    Note2: I'm being asked to do this with no formal teaching in Node or json. The professor said to do this, and search online for "easy" tutorials. Unfortunately, server-side is a weakness of mine.

    The idea: Make a calender, have it read events from a file passed by a server (so I'm writing an html, a js server, and json/xml/etc.). I have the server running, the calendar showing, but the events aren't on the calendar. Easy stuff over, now I need to know what I've done wrong. Here's the relevant code, and what I've picked up from random net tutorials:

    Calendar constructor:
    Code:
    <script>
        $(document).ready(function() {
          $('#calendar').fullCalendar({
          events: 'http://localhost:8080/tutorTimes.php',
          weekends: false,
          weekMode: 'liquid'
          })
        });
    </script>
    php file (one of 2 places I'm sure I'm wrong, this is indeed the whole file):
    Code:
    [
    {"title":"Monday Session","start":"Mon, 29 Apr 2013 12:00:00 EST","end":"Mon, 29 Apr 2013 13:00:00 EST","allday":false},
    
    {"title":"Tuesday Session","start":"Tue, 30 Apr 2013 12:00:00 EST","end":"Tue, 30 Apr 2013 13:00:00 EST","allday":false},
    
    {"title":"Wednesday Session","start":"Wed, 1 May 2013 12:00:00 EST","end":"Wed, 1 May 2013 13:00:00 EST","allday":false},
    
    {"title":"Thursday Session","start":"Thu, 2 May 2013 12:00:00 EST","end":"Thu, 2 May 2013 13:00:00 EST","allday":false}
    ]
    and finally, server (second place I'm sure I'm wrong):
    Code:
    var http = require('http');
    var fs = require('fs');
    http.createServer(function (request, response) {
        console.log('request starting...');
        fs.readFile('./tutorTimes.php', function(error, content) {
            if (error) {
                response.writeHead(500);
                response.end();
            }
            else {
                response.writeHead(200, { 'Content-Type': 'text/php' });
                response.end(content, 'utf-8');
            }
        });
    }).listen(8080);

    so can someone tell me what (probably simple, knowing me) mistake I've made? I wouldn't ask if I had any ideas left myself, and it's due tomorrow night.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Your so-called PHP file is really a JSON file, you know. It has no PHP code in it. It would be better named "tutorTimes.JSON".

    And the content-type is clearly not PHP. I'd call it text/javascript.

    Can't help you with the Node code, per se, though. Have never used Node, though I can make sense of it from your attempt.

    It seems kind of pointless code, since your jQuery code could just as easily read the "tutorTimes.JSON" file directly and get the same effect. The only thing the Node code seems to be doing is handling the request on port 8080 instead of the standard port 80. And at that it's doing it clumsily, since it doesn't even read what the request actually is and always responds with the contents of that "tutorTime.whatever" file. So if your jQuery code asked for "'http://localhost:8080/figNewtons.pdf" your Node code would still serve up the same content.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    Reaga (04-30-2013)

  • #3
    New Coder
    Join Date
    Jan 2013
    Posts
    22
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Well it seems like it's definitely the Node, as you've confirmed what I'd been worried about with the JSON file and it didn't change anything. The problem is I don't know the syntax to make the file readable. I'll attempt to search more on Node, but right now this place and Google are my only real hopes.

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,130
    Thanks
    12
    Thanked 332 Times in 328 Posts
    Quote Originally Posted by Old Pedant View Post
    The only thing the Node code seems to be doing is handling the request on port 8080 instead of the standard port 80.
    that is just a matter of definition, you could easily let the Node server listen to port 80 by changing the call to .listen(80) (if that port is free, sometimes you also have Apache or Skype running that also listen on port 80, so most tutorials use an alternate HTTP port)


    Quote Originally Posted by Old Pedant View Post
    And at that it's doing it clumsily, since it doesn't even read what the request actually is and always responds with the contents of that "tutorTime.whatever" file. So if your jQuery code asked for "'http://localhost:8080/figNewtons.pdf" your Node code would still serve up the same content.
    I guess it needs getting used to writing the whole server instead of a server-interpreted script (like PHP).

    But I agree that using jQuery would suffice.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #5
    New Coder
    Join Date
    Jan 2013
    Posts
    22
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Dormilich View Post
    But I agree that using jQuery would suffice.
    I agree, but the professor specifically requested we write a server for this. Again, after NOT teaching the syntax. He went over "what we can do with Node" for a class, and that was it.

  • #6
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,130
    Thanks
    12
    Thanked 332 Times in 328 Posts
    Hm, the syntax of a Node server is not different from regular JS, the only difference is that you use a completely different API than DOM or HTMLDOM.

    regarding what Old_Pedant said, it would be a good idea to let the Node server handle specific requests* (if that is possible/sensible with the given JSON). you could look into the Express framework, that eases Routing in Node.


    * - here it doesn’t even matter if the request is made through URL parameters (url.html?query=some_value) or if you use "rewritten" URLs (ulr/query/some_value or url/some_value).
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #7
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    since your page is not coming from node, you won't be able to use ajax on it, due to the same-origin-policy.

    add cors to your node server, it's a lot easier to do than with apache:

    Code:
      response.writeHead(200, {
        'Content-Type': 'text/html' ,
       'Access-Control-Allow-Origin': '*',
       'Access-Control-Allow-Methods': 'GET'
    });
    or use JSONp :

    Code:
      response.end( "incoming(" + content + ")", 'utf-8');
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  • Users who have thanked rnd me for this post:

    Reaga (04-30-2013)

  • #8
    New Coder
    Join Date
    Jan 2013
    Posts
    22
    Thanks
    5
    Thanked 0 Times in 0 Posts
    The cors worked, the events show. Now I just need to make the events clickable, and that's simple enough I shouldn't need your help. Thank you, you've made turning this in on time feasable.

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Quote Originally Posted by Dormilich View Post
    Quote Originally Posted by Old Pedant
    The only thing the Node code seems to be doing is handling the request on port 8080 instead of the standard port 80.
    that is just a matter of definition, you could easily let the Node server listen to port 80 by changing the call to .listen(80)
    Yes, I knew that. I was pointing out that the Node code isn't actually handling the URL that was requested. It is responding to *ANY* request to port 8080.

    Which may be fine for this exercise, but isn't a very robust Node server, then.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •