Last week, I came into contact with the sails framework by building a CMS system and learned some of the concepts of ORM. This week, we started to go deep into the 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 my senior is amazing. But the complexity has also increased. My task in the DTree project is to use sails to interact with business processes and databases. The next time this week is to use sails.
Introduction to sails
Sails is like ruby on rails, and it is also a framework to make web development simple. As far as I know during this period, it completely inherits some APIs of Express&Socket.io, and uses waterline (ORM) for unified database interaction, so that CRUD operations can be completed in different database environments without directly modifying code; synchronizes the data of the front and back ends with the backbone framework, and uses the policies middleware to safely verify the interaction of its own blueprints. It can interact through the RESTfull API without a single line of code in the front and back end.
Socket.io & Express
The front-end has learned these two things in time, one is websock communication and the other is to process HTTP requests. Sails is not redesigned but uses these two tools to deal with these functions directly, reducing learning costs a lot. Socket.io triggers messages, listens for messages and performs operations accordingly on both the server and the client.
The code copy is as follows:
`
// Server side
io.sockets.on("connection", function(sock){
sock.emit("normalMessage",{a:"c"});
});
//Customer service
sock.on("normalMessage", function(json){...});
`
The function of Express lies in routing operations, such as app.get('/login', function(req, res){});.
Sails Blueprints & Backbone
In web development, CRUD is a similar type of operation, such as get/post in http and select/insert in database. The blueprint API of sails and the backbone framework of js both use a unified class of functions (such as findOne), so that the internal logic is already established when the program establishes the model and the corresponding controller. For example, the commonly used users model has built a controller in sails to pass and store users' data. All we need to pay attention to is the specifications and business processes of file names. It is also OK if you want to override (overload) the original method. After we set the action and controller in the routes in the config folder, we can add the functions we need to the corresponding controller.
Waterline
The API of sails dependent package is used to interact with databases, such as create(), findOne(), update(), etc. As mentioned earlier, we don’t need to worry about different database tools to call these methods directly uniformly. For example, when entering data
The code copy is as follows:
`
Users.create({username: username, password: password}).exec(function(error, user) {
if (error) {
res.send(500, {error: "DB Error"});
} else {
req.session.user = user;
res.send(user);
}
`
The default system is to save data in the local file system. If we need to choose mysql or mongoDB. Just modify it in the configuration file and select the database adapter to interact with the data.
Policies
Applied to the middleware on the controller, run before the http request is issued. Identity control can be achieved, for example, you can only perform the next actions if you log in.
The code copy 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 successfully got started, but it still needs to work harder on this. Only by laying a solid foundation can we achieve specific needs. So we need to continue learning, find more routines to learn sails in depth, and also know the limitations of this framework.