• Meteor

Server Side Routes

From the class:  Using Iron Router

Server side routing in the new router has gotten a complete overhaul. So in this video, I'm going to show you how to create server side route, and I'll also show you how you can plug in connect middleware directly using the router.

Let's say that we've integrated with Stripe, and we want to connect with or set up an endpoint for Stripe to send its web hooks. This is a pretty common use case for integrating with other websites. So what we need to do is to create a server side route that another website can post data to.

So in this example, I'm going to create another route, and we'll call it Web Hooks Stripe. So that'll be the path. And we can give it a name of Web Hooks Stripe. And what we need to do here is to specify that we want this to run on the server. And so to do that, we can say Where Server, and that will tell Iron Router that this is a server route.

Now, by default, this will setup a route for a get request, and it will look for a route controller named Web Hook Stripe that we'll create in a second. Iron Router now supports restful routes. And so if we wanted to, we can define a route for the post request. And so I could say that for a post request, I want to go into this function. Or I could specify a method on the action controller or on the route controller.

So I might say that we want all post requests to go to the method named Action on our route controller. But for now, to make things simple, I'm just going to go ahead and provide a function directly in here. And just remember that this inside of this function will be an instance of an anonymous route controller.

So to start us off here, what I'm going to do is just respond with some text. And so I'll say this.response, and then we'll put a line break there. So this will end the response and send down some text. And for now, let's actually change this to a get.

And we can also add multiple verbs like this. So now we actually have one route here, but responding to two HTTP verbe-- one for get and one for post. So let me say that this is get, and we'll do the same thing down here. And then we can open up another window and use curl to try out this new route. Since these are server only routes, I'm just going to use curl over here to try them out.

And so to start us off, let's try to send a get request to the web hook stripe route. So I'll say curl, and then -i so we can see the headers that are sent back. We want to send a request to local host 3000 web hook stripe. So this will send a get request.

So here, the headers that are sent back with a 200 OK status, and here's our text printed to the console-- Get Hello World. Next, let's send a post request to the same path. Next, let's send a post request to the same path. And to do that, I can say curl. We'll use the -i flag again. And then -d tells curl to send an HTTP post request. Then I'll use a --, because we're not actually going to send up any data, and then pass the path that we want to send this to.

In this case, we sent up a post request, and we got back a result from the post handler function. You can access the NodeJS response object using this.response, and it has all the methods that a normal NodeJS response would have. And you can access the request object by using this.request.

Now, I'd like to convert this over to a controller. But before we do that, I'd like to add some middleware to our router that will allow us to parse input that we get from the client. So for example, if it sends some JSON data to our web hook, I'd like that to be parsed automatically so that we can access it. Now, this is such a common use case that this middleware has been built into the router itself. Let me show you how to use it.

You can call a method on the router called Use, which works just like with Connect Middleware. So we can pass Connect Middleware directly into this function, and it should work fine. And in this case, because we want to be able to parse JSON that gets posted to this route, what we can do is use the router body parser, and then call the JSON method on that.

So this will automatically now parse out any JSON that gets posted. And we can access it using the request body. I've gone ahead and added some code down here to the post handler. What I'm doing is grabbing the JSON from the request body, and this will be automatically parsed into a JSON object or a JavaScript object. And then what I'm doing is responding right back to the requester saying you posted the string version of this object here. And we get this automatic parsing of the JSON object because we added this middleware here using the Use method.

So let's try this out by using curl. I'll type curl, and then -i for the headers. And this time, for the data, I'm going to pass up a test JSON string like this. And make sure to include a header that tells the server that this content type is application JSON so it knows what to do with it. And then we'll send that request as usual to our Stripe web hook endpoint. So that worked properly. Here are the headers, and here's the response body that includes the JSON data that we posted up to the server.

Next up, let's move this into individual action functions on a dedicated controller. So I've removed these function handlers. And instead, I'm going to specify the name of the method that I want to call on the route controller for this particular HTTP verb. Now, we can define this controller in the Both folder so that it's available on both the client and the server. But in this case, I just want this to be on the server. I don't want it to go to the client at all. And so I've put it in the Server directory under Controllers.

Then I created the outline for the function here, and it still extends from Route Controller. And then I defined these two methods, one for each of the actions that I specified in the route. So up on top here, I filled in these two methods-- the Get and the Post function. And down below, we can use Curl again to send a post request and just make sure that everything is still working.

In this video, I showed you the more powerful new features of server side routing in Iron Router, and I showed you how to create restful routes, use Connect Middleware, and use the Body Parser middleware that comes with Router in order to automatically parse post data like JSON, forms, or even XML.