Sails framework Learning Guide and sails framework Learning Guide

Source: Internet
Author: User
Tags ruby on rails

Sails framework Learning Guide and sails framework Learning Guide

Last week, by setting up a CMS system, we came into contact with the sails framework and learned some ORM concepts. This week, I started in-depth background data interaction and found that the data structure of the twenty framework has been designed on sails (such as node and category). I have to say that Shi Ge is amazing. However, the complexity is also improved. My task in the DTree project is to use sails to interact business processes with databases. Sails will be used in the next part of this week.

About sails

Like ruby on rails, sails is also a framework to make web development simple. As far as I know this time, it fully inherits Express & Socket. i/O APIs, and the use of waterline (ORM) for unified database interaction, so that CRUD operations can be completed without directly modifying the code in different database environments; synchronize the front-end and back-end data with the backbone framework, and perform security verification and Interaction Using the IES middleware. In the front-end and back-end, blueprints can perform data interaction through RESTfull APIs without a line of code.

Socket. io & Express

The frontend time has learned these two things: one is websock communication and the other is processing HTTP requests. Instead of being re-designed, sails directly uses these two tools to handle these features, reducing learning costs. Socket. io trigger messages, listen to messages, and perform relevant operations on both the server and client.

Copy codeThe Code is as follows:
'
// Server
Io. sockets. on ("connection", function (sock ){
Sock. emit ("normalMessage", {a: "c "});
});
// Customer Service
Sock. on ("normalMessage", function (json ){...});
'

Express is used for routing operations, such as app. get ('/login', function (req, res ){});.
Sails Blueprints & Backbone
In web development, CRUD is a type of similar operations, such as get/post in http and select/insert in database. Sails's blueprint API and js's backbone framework both use a unified class of functions (such as findOne), so that the internal logic has been established when the program creates a model and the corresponding controller. For example, the common users model has a self-built controller in sails to transmit and store users data. We only need to pay attention to the specifications and business processes of file names. It is also possible to overwrite (reload) the original method. After setting the action and controller in the config folder, you can add the required functions to the corresponding controller.

Waterline
The sails dependent package API is used to interact with the database, such as create (), findOne (), update (), etc. As mentioned above, we don't need to care about how different database tools call these methods in a unified manner. For example, when data is input

Copy codeThe Code is as follows:
`
Users. create ({username: username, password: password=cmd.exe c (function (error, user ){
If (error ){
Res. send (500, {error: "DB Error "});
} Else {
Req. session. user = user;
Res. send (user );
}
`

By default, the local file system saves data. If you need to select mysql or mongoDB. You only need to modify the configuration file and select the database adapter for data interaction.

Policies

Middleware applied to controller and run before the http request is sent. Identity control can be implemented. For example, you can perform the following actions only after logging on.

Copy codeThe Code is as follows:
`
If (req. session. user ){
Var action = req. options. action;
If (action = "create "){
Req. body. userId = req. session. user. id;
Req. body. username = req. session. user. username;
}
Next ();
} Else {
Res. send ("You Must Be Logged In", 403 );
}
`
Next

Sails is easy to use, but you need to work harder on it. Lay a solid foundation to meet specific needs. Therefore, we will continue to learn more. We need to find more examples to learn about sails and also know the limitations of this framework.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.