📜 ⬆️ ⬇️

Features of using Sails for beginners (Part 2)



Synopsis


This article contains helpful tips for newbies on how to use the various MVC features of the SailsJS framework. These tips can be useful to newbies by discovering some “secrets” that they may not be familiar with - because of this, many people write a lot of crutches and bicycles, this can make their enthusiasm to learn this useful tool go out only because their main forces will be directed on the usual routine that can be easily circumvented using simple and convenient methods. Interesting?



')

Uploading files to the server in SailsJS


In Sails from version 0.10, a new convenient option is built in by default - uploading files to the server using simple and convenient tools familiar to anyone who has ever used this framework. We are all accustomed to accepting GET or POST request data simply by using the param () request method
var page = req.param('page'); //  'page'     

But this method has a flaw - it takes only a string. And we need to work with files, what can we do? Sails has a file () request method for this.
 req.file('file'); //  file -    

Next, the Skipper (Skipper) library created by the Sails team enters into processing, which has a simple and easy to understand API. Now I will give the code of a simple controller, and then I will describe in more detail how to use the API.
 module.exports = { index: function (req,res){ res.view(); }, upload: function (req, res) { req.file('file').upload({ dirname: 'uploads/', },function (err, files) { if (err) return res.serverError(err); return res.json({ message: files.length + '  () !', files: files }); }); } }; 

Index attribute - simple page display. Further unloading - which requests the file (s) sent by the request. The upload () method takes 2 agruments - an options object, and a callback function with data - an error, and files. In our case, after uploading to the server, json responds with data on the number and size of files. Files are sent as a request from a form, as well as AJAX - but with the indication of the necessary meta-information. In its simplest form, the form view looks like this:
 <form action="/file/upload" enctype="multipart/form-data" method="post"> <input type="file" name="avatar" multiple="multiple"><br> <input type="submit" value=""> </form> 


Options

In the options object that we passed, it can contain 2 values:

dirname (string)

The path to the folder where to download files. (the default is ".tmp / uploads)

saveAs (kolbek)

The function to determine the logic of the formation of the names of the downloaded files, otherwise, if not specified - when a file appears with the same name as the old one: the file is overwritten. The base argument is the file object. The function must return the file name. Example:
 function (file) { return Math.random() + file.name; } 


Additionally

Also, in addition to this, you can use additional features as a method for flexibility:
 req.file('file') //   .on('error', function onError() { ... }) //    .on('finish', function onSuccess() { ... }) //  .pipe(receiving) 


Skipper library page on github.



Sails.log - logging in sails


When you need to designate any moments in the console during development, the standard console.log () command will not be as effective as sails.log, the console logging system built in sails. These are small “sugar” features that allow you to highlight your messages in the console in color - and what type of messages they belong to. This is useful when a lot of logged actions are performed - and it is unclear which of them belongs to what: To an error or an informational message, or maybe even just as empty information. For sails.log there are 6 definition methods. Here they are:
 sails.log.error(''); //      error: sails.log.warn(''); //      warn: sails.log.debug(' '); //      debug: sails.log.info(''); //      info: //    : sails.log.verbose(' '); //     ( ) sails.log.silly('  '); //      


Finally


This concludes the second part of our excursion into the possibility of Sails, and their practical use. The next article will cover such issues as “Unit testing parts of an Sails application using Mocha” and other useful points using the SailsJS framework.

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


All Articles