📜 ⬆️ ⬇️

Features of using SailsJS for beginners (Part 1)



Synopsis


This article contains useful for beginners methods and development methods on SailsJS, which may be unknown or incomprehensible to those who have just stepped onto the development path in this convenient and multifunctional framework. It can be all - from the usual commands, generators or services, and ending with the built-in query methods. Here are collected useful trivia platform that can protect you from writing crutches or bicycles. Interested?



Using CoffeeScript in the backend


Maybe not everyone knows, but in Sails not only the automatic compilation of CoffeeScript is implemented not only in the frontend part, but also when writing other system components. To use this feature, you need to install coffee-script in the local environment of the project:
npm install coffee-script --save 

Next, you need to slightly fix the app file , js in the project root. At the beginning of the file (before declaring the sails variable) add the following line.
 require('coffee-script'); 

These were small manipulations to rule out a built-in problem that might introduce some into a stupor.
And now for the generation of the required API for Coffee, in Sails there is an argument --coffee
')
Entire API generation (model + controller)

 sails generate api post --coffee 

where post is the name of our api.

Model generation

 sails generate model post --coffee 


Controller generation

 sails generate controller post --coffee 


With the help of such simple manipulations we included support for Coffee in SailsJS, it works in version 0.10x. In off. documentation is not described.


Built-in Basic Answer System


I think many of us faced the situation when it was necessary to create or use a page with a multiple use answer, starting with a simple 404 error response, and ending with the formation of any of the input data without using controllers. For these cases, Sails provides a response system which is located in api / responses . From the default embedded responses can be considered:

These are not just status handlers like res.send (404), but a fully formed response representation - error pages that you see for example when opening a page not specified in sails or erroneous processing, and receive a visual response of error 404 or 500 with the error text. An example of use.
 res.notFound(''); //   404     ( ) res.notFound('', '/'); //   ,        

In the base case, if you just pass one argument with the error text, it will look something like this:

These are simple and handy utilities that some newbies may overlook. Also, if you conduct a small audit of the code of these answers (which is very simple and well commented out) you can easily compile your own answers. You can read more about the answers in Sails here .


Services


When we want to process something regardless of a single controller, something that we want to use many times, or make a convenient wrapper for any library or module, we can use SailsJS services for this. Services are easy-to-use modules with which you can work locally in your application. All services are located in api / services , many skills are not required to use services. Now spread out on the fingers. Suppose we want to create a custom service with the N method it performs, and then call it from the controller. To do this, create the CustomService.js file in api / services , now in this file we need to define the N method - for this, such simple content is enough.
 exports.N = function(opt1, opt2) { //   N -  ..... } 

You can call the N method in any of our controllers.
 CustomService.N(); 

As you can see, everything is extremely simple and clear, also using the services you can successfully and clearly create an API for generators.


Configurations


Some of the beginners make a non-lethal but very rash mistake - they store the values ​​of some custom variables or objects to configure their applications in the controllers themselves (or generally store them as the defaultsTo attribute in the models). Instead, it is wiser to store all your configurations in the place set by the framework - config /. Now we will look at how to quickly create and use configurations. Suppose we want to create a Custom configuration with 2 stored parameters - N and S, and then use them in our controller. To do this in config / create a file custom.js , with such content.
 module.exports.custom = { N: '.....', S: 'custom' } 

To access the configuration from any part of your application, just use the simplest tools.
 var N = sails.config.custom.N; var S = sails.config.custom.S; 


Finally


This concludes the first part of a small addition to the introductory course for beginners in SailsJS. The next article will discuss such useful questions as “Implementing Uploading Files to a Server Using Standard Sails Tools” and many other features that have appeared in Sails since version 0.10.

Source: https://habr.com/ru/post/228253/


All Articles