Learning Guide for sails framework _node.js

Source: Internet
Author: User
Tags findone http request ruby on rails

Last week, by building a CMS system to contact the sails framework, know some of the ORM concept. This week began to delve into the background data interaction, found that the twenty framework of the structure of the sails on the design (such as node, category), have to say Shige is severe. But the complexity has also increased. My task in the Dtree project is to use sails to interact with the business process and the database. The next time this week is to use sails.

Sails Introduction

Sails, like Ruby on Rails, is a framework for making web development easier. My understanding of this time, it completely inherits some of Express&socket.io's APIs and uses waterline (ORM) for unified database interaction, making it possible to complete CRUD operations in different database environments without having to modify the code directly And the data of the front and back end of the backbone framework, using the policies middleware for security verification The interactive blueprints can be used for data interaction through the Restfull API without a code line in front and rear.

Socket.io & Express

The front-end time has studied these two things, one is websock communication, one is processing HTTP request. Instead of sails, the two tools are used directly to handle these functions, reducing the cost of learning a lot. Socket.io triggers messages on both sides of the server and the client, listens for messages, and makes actions accordingly.

Copy Code code as follows:

`
Server-side
Io.sockets.on ("Connection", function (sock) {
Sock.emit ("Normalmessage", {a: "C"});
});
Customer Service End
Sock.on ("Normalmessage", function (JSON) {...});
`

The role of Express is in routing operations, such as App.get ('/login ', function (req, res) {});.
Sails Blueprints & Backbone
In web development, CRUD is a class of similar operations, such as Get/post in HTTP, Select/insert in a database. Sails's Blueprint API and JS's backbone framework all use a unified set of functions (such as FindOne), which makes it possible for the program to establish the model and the corresponding controller when the intrinsic logic is established. For example, the commonly used users model built controller for data transfer and storage of users in sails. We need to note that only the file name specification and business process can be. If you want to overwrite (overload) The original method is also OK. After we set up the action and controller in the Config folder, we can add the functions that we need in the corresponding controller routes.

Waterline
Sails this dependency package API is used to interact with the database, such as Create (), FindOne (), update (), and so on, as mentioned earlier, we do not need to care about the different database Tools directly unified call these methods. Like when you enter data

Copy Code code as follows:

`
Users.create ({username:username, Password:password}). exec (function (error, user) {
if (Error) {
Res.send (+, {error: "DB error"});
} else {
Req.session.user = user;
Res.send (user);
}
`

The system defaults to the local file system to save the data, if we need to choose MySQL or MongoDB. Simply modify it in the configuration file and select the adapter of the database to interact with the data.

Policies

Applies to the middleware on the controller and runs before the HTTP request is issued. You can achieve identity control, such as you can only log in to carry out the next action.

Copy Code code 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 is logged in", 403);
}
`
Next

Sails is successful, but he has to work harder on this. Lay the foundation in order to achieve the specific needs. So we have to continue to study, to find routine in-depth study sails, but also to understand the limitations of this framework.

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.