node. js and sails~ project structure and MVC implementation

Source: Internet
Author: User

Sails is a node. JS middleware architecture that helps us build Web applications easily, URL: http://www.sailsjs.org/, which is developed primarily on the basis of the express framework, expands the new functional components, Let's take a look at the installation method

One installation sails

Npm-g Install sails

Second, establish a sails project

New TestProject

Three startup projects

CD Testprojectsails Lift

The structure of the four projects, based on the MVC concept

We can see that it is made up of Model,view,controller, which is very similar to. NET MVC, except that the model in. NET MVC refers primarily to viewmodel, whereas in sails the model is mostly data models, That is, the entity in. NET, which is an abstraction of data tables, sails provides many kinds of data persistence, such as local files, Mysql,mongodb,redis, etc., for SQL Server We can also find third-party components.

Five to render the view through the Controller's action

In. NET MVC, we all know that views are rendered using the Render method of the action, which is also the case with sails, which can use native render, or use the encapsulated view method, and use the object returned by your action directly on the view.

Controller/action's Content

module.exports={    index:function (req, res) {        return res.view ("Test/index  ", {title:" uncle ", Engtitle:"Lind"}) ;         // return Res.view ("view_name", data) // The view_name parameter is empty to indicate the current action     }};

View-ejs's Content

<p> objects returned from Action-title:<%=title%></p><p> object returned from action-engtitle:<%=engtitle%></p >

The result of the call is as follows

If you enter the index page, you can write the controller name directly

If the other action wants to take a route such as/test, it needs to be configured in Config/route.js, such as adding a route to/user for the add action, which is set as

NULL }},' Get/test ': {view: ' Test/index ', locals:{layout:null}}

Well, to such a simple MVC of the demo is done, the next section we will refer to model, the data persistence mechanism, for the curd operation of the data table, please look forward to ...

node. js and sails~ project structure and MVC implementation

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.