Start a free Courses trial
to watch this video
In this video we expand upon the simple HTTP server example, and create a web service that parses the URL to perform simple functions.
This video doesn't have any notes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up[?mellow guitar music?] 0:00 Think Vitamin Membership - Est. 2010 membership.thinkvitamin.com 0:03 Node.js - Simple Web Services with Jim Hoskins 0:07 In the previous video, we created a simple http server that returned static text. 0:12 Now we're actually going to look at the url of the request 0:17 and create a web service that actually does something. 0:20 So we're going to build a simple web service. 0:23 I'm going to take the code from our last video here, 0:26 since it serves as a good skeleton for an http service, 0:29 and I'm going to start a new file for our mass server. 0:32 Now, what I want this web service to do--is it's going to be a pretty simple service. 0:35 I want to be able to pass it url's like /add/2/2 0:41 and it will add the numbers 2 and 2 together 0:47 and we'll have different operations, like subtraction, multiplication, and division, 0:50 so it's going to take two values 0:55 and an operation, and then perform that operation on those two values. 0:57 It should be pretty straightforward to implement. 1:00 The first thing you want to do is figure out 1:03 how to actually read our path name, because 1:06 that's where the specifications for what service we want to perform is held. 1:09 Let's fire this up and see what we get right now. 1:13 We're just going to call node math_server.js 1:18 and we get our server up and running and switch to our browser. 1:22 We see our code from last time, so let's try to type in a url here, like add/4/4. 1:26 We just get the same string back, because right now 1:34 our code doesn't actually even look at the url. 1:38 So how can we get that information out? 1:41 Well, we can look at the documentation, but let's just take a look 1:43 at the request object, because I'm sure that the information about the url 1:46 is probably in there. 1:50 So I'm going to use the sys.puts() method 1:52 and inside of that I'm going to call (sys.inspect(). 1:57 What inspect does, is it takes an argument and it will return 2:01 a string representation that allows you to see inside that object. 2:04 I'm just going to inspect our request here. 2:08 We'll see if we can't find out where the actual path here is stored 2:11 inside of our request object. 2:14 I'm going to save it out, and we'll go ahead and restart the server 2:16 because we changed the code. 2:20 Let's refresh this page. 2:22 Alright--so now we see a huge dump, which is all of the attributes 2:25 that are in the request object, and somewhere in here, 2:29 we'll probably see something that looks like 2:32 our add/4/4, so in the request.url attribute, 2:35 we get this string that's 'add/4/4'. 2:39 So let's try to use that. 2:42 Switching back to our code, let's set up some variables inside of our request here-- 2:45 so var, and we'll split our url on the slashes to get the different parts of it. 2:50 We'll just say parts = req.url.split 2:56 and we'll split it on the slash ("/"); 2:59 Let's take a look at what those parts are. 3:08 So down here we'll inspect it, 3:11 and we'll go ahead and restart our server 3:14 and refresh the page to send the request again 3:17 We see actually two items here--the first one looks like our original request, 3:20 where we get--because there was a leading slash, the first element is a blank string, 3:25 and then we get the 'add' string, the '4' string, and the '4' string. 3:29 You'll see there's another one down here. 3:34 There must have been another request in the background 3:36 for the favicon, and that will be important in a second. 3:39 Let's try to get the operation from this part's variable. 3:45 We saw that our op would be in parts sub one [1]; because 0 was our empty string, 3:50 and then index 1 would be the name of our operation. 3:59 Then we'll go ahead and say 'a' is parts sub two [2]; 4:04 and 'b' is parts sub three [3]. 4:12 So to be able to handle different operations, we're probably going to 4:17 just want to create some different functions to handle 4:21 the add, sub, multiply, and divide services. 4:23 So actually at the top of the document here, I'm going to create an object 4:27 called 'operations' and it will have the keys be 4:30 the name of the operation, so add: and the value will be a function 4:35 that takes (a,b) and it does that operation, so we're going to {return a + b}. 4:41 You could imagine, the other ones are going to be pretty similar, 4:50 so we'll just add those in real quick, so our subtraction 4:53 will subtract 'a' from 'b,' our multiplication will multiply, and our division will divide. 4:56 Instead of just getting the string part of the operation, 5:02 we actually want to look it up in this table, 5:05 so we're going to say operations and this part should be a string that's either 5:08 add, sub, multiple, or div, and so 'op' will actually be a function 5:14 that takes two arguments and returns the result of that operation. 5:17 You'll notice all of our functions are expecting numeric values, 5:22 so there's +, -, x, and /, 5:25 bur right now our parts are going to be strings. 5:29 What we actually want to do is call parseInt on this 5:32 will do with erratics 10 just to be sure, 5:38 and we'll do the same thing down here. 5:41 This will make sure that our 'a' and 'b' are integer values. 5:50 So now, all we need to do is get our result, 5:55 so let's say var result = 6:00 so we'll take our operation, which is a function, 6:04 pass it 'a' and 'b' and I'm going to actually change this up a little bit. 6:07 I want this to return 'text/plain' 6:12 and we can get rid of this .write call here. 6:18 Let's just try to return our result. 6:21 I'm going to flip over here, and let's go ahead 6:29 and kill this server, clear out this buffer, and we'll start again. 6:33 Let's see how far we've made it through this code. 6:37 Let's go ahead and refresh it, and we see web page not available. 6:41 Something probably went wrong, so let's take a look 6:45 and here is the error stack. 6:48 We see that I accidentally put parentheses instead of square brackets. 6:50 I meant to do a lookup, not a function call, 6:54 so let's just change that--that should be an obvious one. 6:57 Let's get to the other errors, so I'm going to go ahead and restart the server 7:00 and refresh it 7:04 and we still have a problem. 7:06 So here, we get that the first argument must be a string, array, or buffer 7:09 and that's on line 27 here. 7:14 So we found out that here we can't pass a number because 7:17 the operation returns the number, but this has to be a string or a buffer or an array. 7:20 We can cast this to a string really easily, just by catenating it to a null string-- 7:27 it's one of the easiest ways to do that. 7:31 Now let's go ahead and restart our server and clear this out. 7:34 and refresh, and let me make this a little bit bigger 7:40 so we can see add/4/4/ did 8. 7:44 Now let's try adding 4 + 6, and we'll notice that it crashed. 7:49 Now, it actually crashed before we made our call, 7:56 and that's because this operation here 7:59 wasn't a function, so how could that happen? 8:02 These passed add, it should be a function. 8:04 Well, you remember that before we saw a request for favicon, and 8:07 that's because our browser will try to request a favicon from that server, 8:10 and so it could not find that. 8:14 We have to make our code resilient enough 8:16 to be able to handle those requests, 8:19 because as soon as an error happens inside of your server, 8:21 it's going to crash your entire server. 8:24 So what we're going to do is say the result and we're going to test 8:26 to see if op is a true value, 8:29 got passed to the operations lookup here, op will be nill. 8:34 So if it's true, will use the turn area operator here, 8:37 we'll perform the operation, 8:41 otherwise we'll return , let's say, 'Error'. 8:43 And so now we can start up our server, 8:46 and let's load this up. 8:57 We see 10, and hopefullly we can get more than one request, 9:00 so let's do 10 +6, equals 16. 9:03 Let's try our other operations out--so multiply 10 x 6-- 9:08 we get 60. 9:12 If we divide, we get 1.6667, and we subtract 6 from 10 and we get 4. 9:15 So there we have a quick and dirty little web service that does some simple math for us. 9:29 And now we've seen how to create a simple web service. 9:33 In the next video, we'll create an http proxy by utilizing 9:36 node's http client object. 9:39 [?mellow guitar music?] 9:42 Think Vitamin Membership - Est. 2010 membership.thinkvitamin.com 9:45
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up