Basics of Node Part 1

2015-03-16

Description

Basics of Node Part 1

Show Transcript

scheduler node server that I'm trying to build so last time just for a quick review we learned the basics of node so and I realized that you should probably see this a little bigger because last time that was pretty small so on here you you install based upon the the node.js website you install discarded I already have it but anyways you install the latest version of node and NPM um for me I actually did the install using homebrew so home home and this is how i would recommend on the mac during the insult homebrew node right so install i follow these commands it's a simple Ruby script and let me paste that into a this this script will go out get the latest version of note if you're on a Mac and bring it in and install install the latest well that one's a home brew and then after you've installed homebrew you'll do this command just follow the follow the instructions from this website and you'll have the latest version of note installed in no time it's pretty quick to do so once you've got node you'll have NPM and once you have n p.m. then you will one to NPM install Resta pi which is the thing that we're using to actually build our our node server so node itself has has a server component right so you could do just a basic node server and if you look at the sample docs for node so if you have you do just this this is the kind of boilerplate bar notes over let me take this out and you can compare the two right so here's the boil for a node server and here's what we have make this sit kind of next door to the other one right so if you take a look at the difference here the biggest difference but this is going to be on the the rest of I the rest of I information versus of standard node server the reason why I like using Resta Phi or expresses another really really common Express Express is great if you want to build out a node endpoint that also acts as a HTTP webserver that that has you know a full-blown web page on it resta Phi is kind of a slimmed down version of that from what i can see that just does the endpoint information and the reason why i like both recipe and express is because they have server gets over top post which are more standard syntax for web servers um that's that's the biggest that's the biggest difference that I'm just double-checking the stream yeah the stream is still up the more running cool arm so there is that information anyways so just double checking the stream one more time okay so i think i've got the stream working properly this time also last time it was a little buggy on my own because I had the I had the bitrate set too high so that's what was causing that so anyways if we go back in to we go back in here so you'll see the slight differences in it if you end up if you end up not wanting to use something like russified to build your end point you'll end up with a bunch of if if this data came in you know you'll do this thing if that data came in you'll do that thing all in all in this create server function it allows you to kind of separate out the beginners the posts everything else into their own functions pretty quickly and easily that's that's the that's the simplest reason to use you know express or rest if I um express is way more complicated and what it can do though it if you follow through with if you really want to build an HTTP I'd point I strongly recommend you look up Express and you follow the basic tutorials for the hello world example for our Express we'll get you up and running really really quickly here we're using recipe so pretty much the same idea right now we have a hello world rest if I um anyways so let's not save that go back here okay so now what we had done in the last one is we had at this test and we could go out here and we could do if localhost and then colon 8080 slash test and you would get back test this is a test right that's not the most useful thing in the world because we really want the ability to do posts so here is a basic post posts are um if you don't know get in post on the on the web server terminology a git is basically you send in a URL with some optional parameters and it grabs back data and sends it to you right that's what we just did with this so you send in the URL test and you get back test this is a test post is you send up data and it updates something on the back end so for our example we're just going to do something really basic and do server post and then we are going to do right upload / test and then request these are the requests in the responses that's what you're giving it and then do that always make sure you have the semicolon at the end right so this index for this is the first the first parameter is the end point the second parameter is a function to call whenever one of our requests has come in at that end point so that's the basics and you can see that in the get method to end point and then function to call whenever that endpoint is hit and then I'm going to do a console for right now we're just going to do a console dot log this is the first time I'm doing that in here so you do console dot log and then you do just say 200 plus request + + 2 + request dot you are oh so how do I know what's valid syntax for this request well thankfully I have sample code but if i didn't have sample code i would go out to you see if rest if I has that rest of I so here's here's some of them right request a program name let me make this bigger for you might have made that a little too big but um here's so I always thought to the documentation I think this is key whenever you're coding just in any language right you go out and you look at no dress applies website and here's one of those samples you you Reza program's name right function respond right I'm simplifying with syntax a little bit so you do this this syntax is instead of let me explain this real quickly so instead of doing the function here they have named the function right which I could easily do here let's just say this test function I wanted to make it more generic I can just pull this function out make sure I have the semicolon there so that this won't blow up right so I just pull this function out and give the function a name test response right belong spawned probably should use the next also as they have in their right so now if i run this again and I will go back to the polls test no change right it's it's just a difference in tax for it right this makes it more generic test response could be called from multiple places at this point in time so the server dog get calls test response and I could also have this over post call test response if I wanted to it's just a way of generic sizing it right you make you build something out into a function instead of having it hindi in in that particular method as its response so both of those methodologies are are fine okay so the next piece of information that we wants of the post right back to our post arm now we have the post method at least built out so can try it out and see how well it works so if i do localhost dash there's a upload load test right so this is going to sake it is not a lack I method not allowed air so that's because I'm trying to I told this that this was a post and I didn't send any data right so I said hey this thing to post I'm expecting data whenever you send it up and what I actually said it was no data so that is going to be key right now I actually have to send a tape of data so to do that I use a tool in chrome called advanced rest client there's many of these restaurant right so let me zoom in on this for you too so maybe sure that you guys can see that yeah so it means rest client right here in this guy he is a locator whatever comes to testing out web server is it simple it's easy i'm i'm a big proponent of chrome and I'm a very big proponent of this and you can see some of the other things I was working on the past let's just do a just blow away this information flow away that information right post okay so then I want to go back here close this close go here alright so I'm copying the URL since I knew that it was right yo 8 i'll upload in the test put this here and then what I'm going to pass it in the payload so right right right here you're going to learn the basics of an HTTP request so an HTTP request especially a post request always has header information of some sort and it has a payload arm and then it has a payload type right setting the content type for our case we're going to do application j song as the Fela tight and you notice I had that set to something else forum URL encoded usually means like this is an upload from a website itself to its web server and then application XML is another type right you have XML which is the funny similar to HTML right XML tags look like that um for our case we're going to use JSON and that's the same guy that we were working with be for this thing that's JSON it's the reason why we moved from XML to JSON a lot of people use XML and soap for a lot of messages soap soap is a version of XML that has strong it's strongly typed so you can give it similar to an object model where you have dot notation right a soap message has as a very specific body and specific attributes that you can kind of reference the reason why we're using JSON is because it is a much much smaller it seems like if you take something from XML and format it into JSON it's like half the file size which is important whenever you're sending payloads over the Internet anything we can do to reduce this helps it was that being said we go down here if we're just going to put in this exact information except we are going to add to quote because I don't think it will like it without it okay so we send that guy right headers payload and said well we got invalid JSON expecting tocate ok so what I sent it was not valid JSON so go back in here and go back to JSON lint where we were last time calm right go back into JSON live com zoom in on it go here and go validate and you will see that it is not valid valid JSON so error on 12 the quotes you'll notice that i was using single quotes and i think this has to be doubled yes so as i theorize i was using single quotes this has to be double quotes so you'll you can tell that in here i'm using no quotes and single quotes and it works fine because it it knows to convert that into double quotes whenever it goes ascendant however whenever are manually sending the data it has to be in the exact right format so in this scenario i remove and I apologize that's probably too small for you guys to see 20 right let me zoom in on all of this so that you guys will see you better so upload test is my endpoint in advance rest client um 8080 and then test this is a test right go in here convert all of these two double quotes and hit Send and my rest server okay is probably not it's up like clear the first one oh you know what it is up so if I close it here right and I try to hit Send I'll get back oh let's go back in the history good back into this one select it all right this is all the correct data and then try to hit send again right with this over not up hit send no response immediately right however if I launch it hit send it should sit there and wait so it's sitting in wait now why is it waiting right and what you'll see here is that I actually did the post to detect a tough to upload and test right so why is it waiting well it's pretty simple I never coded out a response so what happened is the server got the request in it's sitting there and said I don't know what to do with this request I'm I don't have a response for so at this point in time the client is actually going to here and hang and wait for a response waiting for a response this is where having your responses come back quickly from the server is extremely important right if I'm doing a whole bunch of logic in here in this post and halting on the logic then all of a sudden you're going to get into this situation where the clients infinitely waiting you see this in all of your apps today where you have an extremely slow response from something right I'm actually gonna close this window see it a little darker okay so let's just see it as that's a little better yeah it might be better might get worse who knows um anyways easier for me to code in a darker room okay so now we have we have this information right we need to send back over our response so let's go here send back a response and instead of sending back a response just send back data and okay right so set the header to content type application JSON and set the response to okay so now if i close this guy you'll notice that immediately I got back no response the second I killed the server and then if I go here activate it again make sure that will save it actually close go here send data okay so now i have a post but the post doesn't do anything right not particularly useful if it doesn't do anything so let's just make it save whatever i have passed up right so what I want to do is I had set up test this is a test right so let's just have a set up um oh let's just end this end up I will still have a send-up test this is a test or test this is a schedule object right so we're going to have its end up tests this is a schedule object ok so then what we're going to do is we're actually going to have to read the data whatever comes in so what I'm going to have to do here and I am looking over to West body so it's going to be like this so in a post the request has a body right so you in in recip I in particular you can just do hoping this works let's just double-check um before I go to coat it all out I'm going to double check and make sure that this is actually the correct sin sexo syntax and then body plus plus stuff body right so I'm adding to the end of this console.log the request off body making sure that I'm getting along all right then I save and close and then i hit send data ok and of course I get back object object not the most useful thing in the world but if I do request up body test right because what I'm doing is I'm sending back an object with a key of tests and a value of this is a this is a scheduled object right so what I'm looking for now is request a body test this guy's should be the key is test a value should be the string value of this is a scheduled object so cancel save and send data ok and you can see this is a scheduled object as what it print it out right that's what's in the body so let's do that right just make it a little bit cleaner and then what I'm going to send back so now how do i store this well we're going to get into more advanced storing and the next probably the next one but um for this one we will just store it at the server level so what i will do is i will do bar test data so that sets it to nothing initially and then i come in here and I do test data equals request stop body test ok so now save this so real quickly what I'm doing here declaring a variable at the top that stores the test data and then i'm reading the test data whenever comes in from this upload the test right close this out here run it and then hit send and you won't notice any changes here but now it's saved and now how do you get to that data right so now i have the server top load up load test and then i have this guy test response what I'll do here is do test test data ok so all I'm doing here is I'm saying let me put these in the order that will make most sense and let me move this back into it right so it's a little bit easier to read I'm just cleaning up the files a little bit for you guys so that you can see exactly okay sorry so let's move this down right so here we go okay I'm just moving the file around for you guys so what I have here is the post i put on top now let's looking for request ipod a test and the risk and the get is now reading in the data so let's just say that for my first one I haven't done this upload yet right and I try to do the gift so if i go back here go and then change this to localhost test right you'll see blank all right you see test and then no data however the second i upload something you see this is a schedule object right so send and now hit run again on this this is a schedule object right and i can go back and change this into me infinitely this is a schedule item right send that one and receive it right so that's great if you have a single item right but it doesn't necessarily do you a lot of good because you know that whenever we receive schedule items we're not going to receive one item in sword that one item we're going to store multiple items so and i think this will be the last thing we'll do for the day but we'll make it so that it can store multiple items so to do that this this test data has to become an array so what you do is and i always have to look up the JavaScript syntax I think it's just new all right javascript and you can see that I've done this a lot of times because i work in multiple languages and the array syntax is different in all of them so bar this is if you're doing a statically typed to right here we go new array right so here we declare this as new array right and the test data is not going to be set to that right test data needs to be set to an array of data so what we're going to do is for right now we're just going to store an array of strings essentially so you're going to have you know just the just the bodies test so all we're storing right now is one key right not storing the total object we're just going to sort this piece arm in the array so what we're going to do here is we go back and we create a new array and then test data get rid of that dot push leaves the syntax and then let's just do test data let's do sub zero for now okay so just a word of warning this will blow up really really bad and actually both will show that first so this is definitely not the way you want to code it and I'll show you why so we reran the server with this change the problem with this is it's going to work great if we go here to advance rest client we go up here we send one and then we hit the end point and a worker right no problems just like before however if I close down the server we launch the server and then I come here first it is actually going to return a blank about the script object which was not what I was anticipating actually so it's smart enough to do this conversion and this is where sometimes node actually blows my mind because in other languages and in other frameworks this would have blown up because you have nothing at the zero index and you're trying to send it back so usually in Java that would cry but in this case it's just returning a blank object which is actually what I want so always good they're not what I was expecting in fact so it's actually pretty good oh and what I'm more interested in so let's reserve test data all right so if I return test data that's a one challenge with returning test data so testy today isn't going to be in the format of an array so I'm pretty interested to see what that does so I get back an array with nothing in it and then if I go here and send refresh and then do it again with a different name right there's a scheduled thing said that actually working as I had hoped so that's easier and know that I would have ever imagined there's in most languages you have to put a whole bunch of if it's if it's nola blank then you send back a blank you know data statement put in here if if this guy because it's not no it's actually a blank all right it sent back like a ray which is pretty cool so it knows enough to do the right thing and this is your sample data so now you have it when you send up a message right so you send up a message with your upload you can now retrieve that message and the previous messages all in one foul swoop I think that'll be all for this tutorial I know it's a lot to take in right you have you have a post method on a server you have a get method on there and that is pretty much the basics of server communication right you send some data up you retrieve some data that's that's the basic fundamentals this is the thing that you'll need to be able to move anywhere with an location now the next piece of information that you're going to need is going to be a way to store this data so this is great but as you saw one of our kill the server and rerun the server all this on your data is going so there's a million ways that you can soar the data and the one in particular that we are going to look at is called um so in the programming pantheon thurs like i said a million ways to sort data and sequel was kind of like the father of the this this type of relational databases is what they were called right in sequel was the language behind it um that was a way of storing data with indexes to each other and and was a very efficient way to sort data and mongodb is kind of a reimagine a shin of that where you're storing things in a more native JavaScript d format object oriented kind of collection based format so MongoDB is is is what we're going to learn simply because it's very quick and very easy to integrate bit node there's also you could also store this data in a flat file right so there's no reason why you can't just say test data output to a output to a file and then bring it all back in that's also acceptable but the problem that you get into that is file box and all the fancy stuff that's within you know os's and you're dealing at the OS level then right where you're writing out a file and reading in a file that'll work but not probably the preferable way to store data long-term something like something like I collected a MongoDB is so that'll be our next topic let me know if there's anything else you'd like to see I will you know I'll be I'll be I'll be putting that together shortly probably and we will go from there once we get this wired up we're going to start on the clients so once once we get an actual schedule object being stored then we are going to go in and wire up clients so that you can actually receive the schedule and then we're going to make it so that it's fancy and you can see different views that kind of stuff so that's the basics for today though right now you have a server running you have the ability to send in you have the ability to receive data those are kind of the primary things that you're going to want to have and that should be pretty good okay thank you for watching today and I will start again later

Community Discussion

Join the conversation on YouTube.

View & Comment on YouTube →