A study guide to the sails framework

  • 2020-05-09 18:05:27
  • OfStack

Last week, I came into contact with the sails framework by setting up the CMS system and knew some concepts of ORM. This week, I began to go into the background data interaction and found that the data structure of twenty framework was designed on sails once again (such as node and category). But it's also more complex. My task in the DTree project is to use sails for business process and database interaction. Use sails for the rest of the week.

sails profile

Like ruby on rails1, sails is also a framework for making web development easy. As far as I know these days, it fully inherits some API of Express& Socket.io, and USES waterline(ORM) for unified database interaction, so that CRUD operation can be completed in different database environments without directly modifying the code. Synchronizing data with the backbone framework, blueprints, which USES policies middleware for security verification, interacts with its own blueprints through RESTfull API without a line of code.

Socket.io & Express

Front-end time learned these two things, one for websock communication and one for handling HTTP requests. sails has not been redesigned, but USES the two tools to handle these functions directly, reducing the cost of learning. Socket.io triggers messages on both sides of the server and client, listens for messages, and ACTS accordingly.


 `
 // The server side
 io.sockets.on("connection", function(sock){
 sock.emit("normalMessage",{a:"c"});
 });
 // The end of the 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 class of similar operations, such as get/post in http and select/insert in the database. sails's blueprint API and js's backbone frameworks both use functions of class 1 (such as findOne), so that when the program establishes model and the corresponding controller, the internal logic is already established. For example, users model, commonly used in sails, has built controller for data transmission and storage of users. We only need to pay attention to the specification of the file name and the business process. You can override the old method if you want to. After setting up action and controller in config folder, we can add the required functions in the corresponding controller.

Waterline
sails, the package dependent API, is used to interact with databases, such as create(), findOne(), update(), and so on. As mentioned earlier, we can call these methods without worrying about different database tools. Like when you're entering data


`
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 of the system is to save data on the local file system. If we need to select mysql or mongoDB, we should choose mysql or mongoDB. You only need to modify 1 in the configuration file to select adapter of the database for data interaction.

Policies

The middleware applied on controller runs before the http request is issued. You can implement identity control, for example, if you have to log in to do the next thing.


`
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 works, but you have to work on it. Lay a solid foundation to meet your specific needs. So I'm going to have to keep going, looking for more routines to go into sails, and be aware of the limitations of the framework.


Related articles: