Node.js and sails ~ Project structure and MVC implementation and log mechanism _node.js

Source: Internet
Author: User

This article begins with the installation of sails to introduce the node.js and sails log mechanism, the small partners have been eager to see the next bar, OK.

Sails is a node.js middleware architecture that helps us to easily build Web applications, URLs: http://www.sailsjs.org/, which is mainly based on the Express framework, expands the new functional components, Now let's take a look at the installation method

One installation sails

Npm-g Install sails

Ii. establishment of a sails project

Sails New TestProject

Three startup projects

CD TestProject
Sails lift

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

We can see that it is composed of Model,view,controller, and that the call relationship between them is very similar to. NET MVC, except that the model in the. NET MVC mainly refers to the ViewModel, while in the sails model is mainly the data models, The entity in. NET, which is an abstraction of the datasheet, sails provides many kinds of data persistence, such as local files, Mysql,mongodb,redis, and so on, we can find the third party components for SQL Server.

Five renders the view through the controller action

We all know in. NET MVC that the view is rendered by the Render method of the action, which is the case for sails, either by using native render or by using the encapsulated view method, and directly using the object returned by your action on the view.

The content of Controller/action

module.exports={
  index:function (req, res) {return
    Res.view ("Test/index", {title: "Uncle", Engtitle: "Lind"});
    Return Res.view ("view_name", data)//view_name parameter is NULL representation with current action
  }
};

The content of View-ejs

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

The results of the call are as follows

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

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

' Get/user ': {view: ' User/add ', locals: {layout:null}},
' Get/test ': {view: ' Test/index ', Locals:{layout:null}}

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

Ps:Node.js and sails~ logging mechanism

See sails's log will think of Log4net, it is true that they are similar in many places, are the way to use the grading record, and sails I feel more convenient to use, it does not require us to do more things, direct sails.log. Level ("Your log Content") is done, You do not need to care about what the single case, or the persistence of the way, sails's log is only a supplement to Console.log, can be understood as a class after the Console.log, and the color of the distinction, hehe.

Let's take a look at the log level of Sails.log.

Level
Priority Log FNS Visible
0 Silent N/A
1 Error .error()
2 Warn .warn().error()
3 Debug .debug().warn().error()
4 Info .info().debug().warn().error()
5 verbose .verbose () ,   .info () , &NBSP; .debug () , &NBSP; .warn () , &NBSP; .error ()
6 Silly .silly().verbose().info().debug().warn().error()

Second, start testing our logs.

Sails.log (' Debug Log! ');//sails.log.debug ("Debug")
Sails.log.error (' ERROR log! ');
Sails.log.warn (' Warn log! ', ' request aborted ');
Sails.log.info (' Info log! ');
Sails.log.verbose (' Verbose log! ');
sails.log.silly (' Silly log! ');

The log level of the three configuration items, located in the Config/log.js

Module.exports.log = {
Level: ' Info '
};

Four from the results, when logging, only log content that is lower than the current configuration level is logged

How, sails's log is quite convenient!

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.