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

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

4.5 Working With Routes

In this lesson we'll see how to add routes to our application in order to tie our controller to a view.

4.5 Working With Routes

Hi folks. In this lesson we're gonna take a look at how we can define custom routes for our application. The sails router is responsible for mapping URLs to controllers. And routes are the individual rules that tells the router which control to use for a given URL or which action within a controller to use based on the URL. Some routes in sales are configured automatically for us by sales, like the routes for static assets. Other automatic routes are the blueprint shortcut and RESTful routes that are generated when we add our model. Earlier in the course, we disabled the automatic generation blueprint shortcut routes. It's recommended to disable these before moving an application to production. Blueprint action routes are also created automatically for all action defined in our controllers. And we actually saw this in action in the last lesson when we added the sign in action to our user controller. And we were using that from Postman. While it is acceptable for these blueprint action routes to be retained in production, it is possible to expose functionality unintentionally. So for this example, let's go ahead and disable these as well. So we ned to do that again in the config/blueprints.js file. And we'll uncomment out the actions property here. And we're just gonna set it to false. Great, so now if we try to use the sign in action that we added earlier in the course from Postman, we'll see the standard no record found with the specified ID message and a status of 404. Let's just give that a try. We want to re-lift our app. And now let's switch back to Postman and we'll try sending in our request once more. So this time we see the message as expected. So what we need to do now is configure an explicit route for this action. And we do this in the config/routes.js file. When we open this file up, we can see that there is a single route defined for slash, which would normally show the homepage.ejs template from the view folder. We overrode this already by placing a regular HTML page at the route of the assets folder. So let's just comment this out. To configure a new route, we need to supply the address of the route as the property name and the target of the route as the value. The address is composed of an optional http method, followed by a URL, followed by a URL path. The target can be specified in a number of different ways, but will commonly point to a controller action or view. So let's add some routes to handle our authentication. We can add a route that matches a GET request to sign in and which targets a sign in template. The target in this case is an object. That object has a property called view, and the value of this property is the name of a template. Now I did say at the end of the last lesson that we were gonna add a sign in page in this lesson. We're not actually gonna do it in this lesson, we're gonna do it in the next lesson. But we can still configure the route for it now. So we can also add a route for the sign-in controller, and this will be invoked instead of GET if we use POST. So the format of this one is slightly different the URL path/signin is exactly the same, but we're gonna use http POST instead of GET and this time the value for this route is a simple string. And that string targets the user controller, and inside the user controller, the sign in action. So it's UserController.signIn. So let's try to re-lift sales at this point. So at the top there we can see a non fatal error which say's that sales is ignoring an attempt to bind the route (/signin) to an unknown view. The view is signin. That's expected and we're just gonna ignore that for now. And let's just get back to Postman, and we should now be able to send our sign in request. We just need to update the request to be a POST instead of a GET. And we see a 404 this time, and the reason why we see the 404 is because we haven't added the sign in template to the views folder yet. So as I mentioned, we're gonna come back and add that in the next lesson. And in actual fact, that should have worked because we changed the http method to POST, so that should still have triggered to our sign in controller. And I think the non fatal error has actually caused that to stop working. So don't worry, we'll come back and carry on where we left on in the next lesson, and we'll get our new view added and we'll see what's happening with this POST request. So, in this lesson we saw how we can disable the automatic action binding by setting the actions property to false in the blueprint.js config file. We also learned how to configure custom or explicit routes and sales, using the routes.js config file. Thanks for watching.

Back to the top