Node. js and Sails ~ Project Structure and Mvc implementation and Log Mechanism, node. jssails

Source: Internet
Author: User

Node. js and Sails ~ Project Structure and Mvc implementation and Log Mechanism, node. jssails

This article first introduces the Log Mechanism of node. js and sails from the installation of Sails. You can't wait to see the following, okay.

Sails is a Node. js Middleware Architecture that helps us easily build WEB applications.

1. Install Sails

Npm-g install sails

2. Create a Sails Project

Sails new testProject

3. Start the project

Cd testProject
Sails lift

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

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

5. Use the controller action to render the view

We are. net mvc, we all know that the view is rendered through the render method of action, so does sails. You can use the native render or the encapsulated view method, you can directly use the object returned by your action in the view.

Controller/action content

Module. exports = {index: function (req, res) {return res. view ("test/index", {title: "", engTitle: "Lind"}); // return res. view ("view_name", data) // If the view_name parameter is null, the current action is used }};

View-ejs content

<P> object returned from action-title: <% = title %> </p>
<P> returned object from action-engTitle: <% = engTitle %> </p>

The call result 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, you need to configure it in config/route. js. For example, add the route as/user for the add action. Its settings are as follows:

'Get/user': {view: 'user/add', locals: {layout: null }},
'Get/test': {view: 'test/Index', locals: {layout: null }}

Now, the simplest mvc demo is ready. In the next section, we will reference the Model, that is, the data persistence mechanism, to perform the curd operation on the data table, coming soon...

Ps: Node. js and Sails ~ Log Mechanism

When you see the logs of Sails, you will think of log4net. Indeed, they are similar in many places, and they all adopt the hierarchical record method. I think sails is more convenient to use, it does not require us to do more things, but directly sails. log. level ("your log Content"). You don't need to worry about the singleton or persistence mode. The log of Sails is only for the console. A supplement to log can be understood as the console after the class is divided. log, which is distinguished by colors.

1. Let's take a look at the log Level of sails. log.

Priority Level Log fns visible
0 Silent N/
1 Error .error()
2 Warn .warn(),.error()
3 Debug .debug(),.warn(),.error()
4 Info .info(),.debug(),.warn(),.error()
5 Verbose .verbose(),.info(),.debug(),.warn(),.error()
6 Silly .silly(),.verbose(),.info(),.debug(),.warn(),.error()

2. test 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! ');

Log Level of the three configuration items, in config/log. js

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

4. We can see from the results that only logs with a lower level than the current configuration are recorded.

How about sails logs!

Related Article

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.