Home  >  Article  >  Web Front-end  >  Node.js and Sails ~Project structure, Mvc implementation and logging mechanism_node.js

Node.js and Sails ~Project structure, Mvc implementation and logging mechanism_node.js

WBOY
WBOYOriginal
2016-05-16 15:36:371235browse

This article first starts with the installation of sails and then introduces the logging mechanism of node.js and Sails. Friends, you can’t wait to read the following, okay.

Sails is a Node.js middleware architecture that helps us easily build WEB applications. URL: http://www.sailsjs.org/. It is mainly based on the Express framework. Developed on the basis of new functional components, let’s take a look at the installation method

1 Install Sails

npm -g install sails

2 Create a Sails project

sails new testProject

3 Start Project

cd testProject
sails lift

Four project structure, based on MVC concept

We can see that it is composed of model, view, and controller. The calling relationship between them is very similar to .net mvc. However, the model in .net mvc mainly refers to the viewmodel, while in sails, the model mainly refers to the viewmodel. It is the data model, that is, Entity in .net. It is an abstraction of the data table. For the persistence of data, sails provides many types, such as local files, mysql, mongodb, redis, etc. For sqlserver, we can also find the third Third party components.

5 Render the view through controller action

We are in .net mvc. Everyone knows that the view is rendered through the render method of the action. The same is true for sails at that time. You can use the native render or the encapsulated view method, and on the view Just use the object returned by your action.

Controller/action content

module.exports={
  index: function (req, res){
    return res.view("test/index",{title:"大叔",engTitle:"Lind"});
    //return res.view("view_name",data)//view_name参数为空表示用当前的action
  }
};

View-ejs content

e388a4556c0f65e1904146cc1a846beeObject returned from action-title:347286aa4c95cae85b99ddcb854c9f7494b3e26ee717c64999d7867364b1b4a3
e388a4556c0f65e1904146cc1a846beeObject returned from action-engTitle:3a37ddac8900833f34740711f4f8f2ad94b3e26ee717c64999d7867364b1b4a3

The result of the call is as follows

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

If other actions want to take a route like /test, they need to be configured in config/route.js. For example, add the route to /user for the add action, and its settings are as follows

'get /user': {view: 'user/add',locals: {layout: null}},
'get /test':{view:'test/index',locals:{layout:null}}

Okay, the simplest MVC DEMO is ready. In the next section, we will refer to the Model, which is the data persistence mechanism, to perform the curd operation on the data table, so stay tuned...

ps: Node.js and Sails~Logging mechanism

When you see the logs of Sails, you will think of log4net. Indeed, they are similar in many places. They both adopt a hierarchical recording method. However, I think Sails is more convenient to use. It does not require us to do more. Things can be done directly at sails.log. level ("your log content"). You don't need to care about singletons or persistence methods. Sails' log is just a supplement to console.log and can be understood as divided into The console.log after adding the class is distinguished by color, haha.

1 Let’s take a look at the log level of sails.log

2 Start testing our logs
Priority level Log fns visible
0 silent N/A
1 error <font face="NSimsun">.error()</font>
2 warn <font face="NSimsun">.warn()</font>, <font face="NSimsun">.error()</font>
3 debug <font face="NSimsun">.debug()</font>, <font face="NSimsun">.warn()</font>, <font face="NSimsun">.error()</font>
4 info <font face="NSimsun">.info()</font>, <font face="NSimsun">.debug()</font>, <font face="NSimsun">.warn()</font>, <font face="NSimsun">.error()</font>
5 verbose <font face="NSimsun">.verbose()</font>, <font face="NSimsun">.info()</font>, <font face="NSimsun">.debug()</font>, <font face="NSimsun">.warn()</font>, <font face="NSimsun">.error()</font>
6 silly <font face="NSimsun">.silly()</font>,<font face="NSimsun">.verbose()</font>, <font face="NSimsun">.info()</font>, <font face="NSimsun">.debug()</font> <font face="NSimsun">.warn()</font> <font face="NSimsun">.error()</font>
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!');



3 Configure the log level of the project, located in config/log.js


module.exports.log = {

Level: 'info'

};

4 As you can see from the results, when logging, only log content lower than the current configuration level is recorded



How about it? Sails log is very convenient!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn