7 days of WordPress plugins, themes & templates - for free!* Unlimited asset downloads! Start 7-Day Free Trial

Next lesson playing in 5 seconds

  • Overview
  • Transcript

3.1 Shortcut Routes

While we're building the front-end, it can be useful to have some data to work with. In this lesson we'll see how to use the blueprint shortcut routes to rapidly add test data for use during development.

3.1 Shortcut Routes

Hi folks. In this lesson we can take a look at Sails Blueprints to see what they are and how they can help us when developing front end of our application. The app that we will be making is a dashboard for viewing telemetry data from another application that we want to monitor. We don't have another application that we want to monitor, but we can easily issue fake requests to our app in order to have a set of data to work with. Sails Blueprints consists of different types of blueprint that you use in different situations. Blueprint shortcut routes, blueprint RESTful routes and Blueprint actions. We'll look at the shortcut routes first, which can be used to create sample data for our application. We can use these from the browser, but first we need to use Sails to generate an API for us, which is done in the command line. So we just need to make sure that the command line is running from the roots of application, so that's the dashboard folder, and we can use the Sails generate API command and tell it the name of the API that we want it to generate. So let's generate one called event. And we should get some feedback to say that sales has created the new API for us. This process results in several new files being created in the API folder in our app. So let's just take a quick look in the API folder. And the folders that get created are a model and a controller. So we can see that there's an EventController.js file, and in the models directory, there's a file called Event.js. Behind the scenes, Sails has also created a series of shortcut routes for us that we can use from our browser's address bar in order to create new records in the event model. In order for that to work, we need to lift the application. So let's do that now quickly. We'll see a new message that we haven't seen before. We're seeing this because Sails has created a model for us, and basically what it's saying is that each time Sails lifts, it will try to do various things to our data for us. So there are several different options that we could choose here, and as you can see, my console has just timed out. So we'll come back to that in just a moment, but these options that we can choose from, they're shown near the top here, one, two, three, safe, alter, and drop. So safe is the production setting. We always want to use that in production, and that just makes sure that none of our data gets lost. For development, we can choose either alter or drop. What alter does is to take the data that is already stored in the model, put it into memory, recreate our database tables, and then reinsert the existing data for us. Or we can choose the drop option, and what that does is just wipe all of our data each time we lift the app, so that we can start from scratch. So, we don't actually have any data yet. It doesn't really matter which option we choose, so I'm just gonna lift the app again. We'll see the message again, and I'm just gonna choose option two. And then Sails will go ahead and lift our app as normal. So, there's an application that I mentioned near the start of the course, in which I prompted you to install, called Postman, so let's open that up now. So when we created our event API and lifted the app, Sails has gone ahead and created some shortcut routes for us. And one of these routes is the create route, and that let's us easily create new records in the model from either a browser's address bar, or from this app Postman. And as we want to send params, it's probably a bit easier to use Postman rather than the browser. So let's expand the Params section of Postman here, and we can just add in some variables. And it looks like I've put the browser one in the wrong place. So as you can see, Postman has automatically updated the URL for us. And we just wanna actual that's the type of the request is set to GET, and then we can send that request. And we should see a nice 201 status at the bottom to say that our new record has been created, and let's just scroll down slightly, we see the response from our app here. And it basically just echoes back the record that was created. So, you can see that the os, type and browser are things that we sent in using the URL, but also we have a createdAt, updatedAt, and an automatic id. So that id will increment automatically every time Sails adds a new record into our model for us. So we've added a single record to our model using the create blueprint shortcut that Sails created for us when we lifted our app. Another shortcut that we can use is the find route. So we can do this either from Postman or we can do it in our browser's address bar. Let's use the browser this time because the request is much simpler. So we can use the find shortcut to find all of the records, and in this case, it returns an array. Let's just close the dom explorer for now. So it returns an array containing all of the records in the model. We only have one, so it's only returned one. But also, we can use the id of a record that we want to find, and we can just update the path in the URL here. So we do that if we want to find the first one, which is the only one that we've got, then we just use id. This time it returns just an object instead of an array. So these shortcut routes map to blueprint actions. These actions, find, create, update, and destroy, are the default actions available to any controller in Sails. And we saw that at the start of the lesson, an event controller was created for us when we generated the event API. So these actions are all generated by Sails automatically, and that will happen for all of the controllers that we have. Both the shortcut routes and the restful routes work in conjunction with the blue print actions. The shortcut routes all use to get requests because they're intended to facilitate rapidly adding records to our model using nothing more than a browser. They're design is to be used quickly and easily, although we use Postman in this lesson to make them even easier. We'll move on to look at the restful routes in the next lesson, but as we'll see the restful routes trigger the same blueprint actions that the shortcut routes that we used today did. So in this lesson, we created an API for our application and saw that Sails created a model and a controller for us. We also learned about the different types of blueprint that are within Sails, and saw how to use the shortcut routes to quickly create new records for our model using just a browser, or a browser extension at least. We also saw that all of the shortcut routes use the GET HTTP method and learned how these shortcuts are mapped to the corresponding blueprint actions. So GET create maps to the create action, GET find maps to the find action, GET update maps to the update action, and GET destroy maps to the destroy action. Thanks for watching.

Back to the top