SAILSJS Learning Note

Source: Internet
Author: User

Menu list:

    • Custom Controller

    • Custom module Use

    • Custom model

    • Custom Middleware

    • Custom Service?




    • Routing with the corresponding controller processing:

      • Change with command line & Controller

Wade-mac:fin_server_invest mac$ sails generate controller mail Sendmaillog

Info:created a new controller ("Mail") at api/controllers/mailcontroller.js!

    • Total Route rollup: config/routes.js

As usual add:, ' Get/make/a/mail ': "Mailcontroller.sendmaillog"

    • Use of modules:--as before

/**

* Mailcontroller

*

* @description:: server-side logic for managing mails

* @help:: See Http://links.sailsjs.org/docs/controllers

*/


Module.exports = {



/**

* ' Mailcontroller.sendmaillog () '

*/

Sendmaillog:function (req, res) {

var log4js = require (' Log4js ');

var logger = Log4js.getlogger ();

Logger.debug ("Some debug Messages");


Return Res.json ({

TODO: ' Sendmaillog () is not implemented yet! '

});

}

};


    • Model command:

Https://www.digitalocean.com/community/tutorials/how-to-create-an-node-js-app-using-sails-js-on-an-ubuntu-vps


$sails Generate model User name:string email:string password:string

$sails Generate Controller User index show edit delete



    • Middleware:

https://gist.github.com/mikermcneil/6255295

Look at Config/http.js



Module.exports.http = {


/****************************************************************************

*                                                                           *

* Express middleware to use for every Sails request. To add custom *

* Middleware to the mix, add a function to the middleware Config object and *

* Add its key to the "order" array. The $custom key is reserved for *

* Backwards-compatibility with Sails v0.9.x apps the *

* ' custommiddleware ' config option. *

*                                                                           *

****************************************************************************/


Middleware: {


/***************************************************************************

*                                                                          *

* The order in which middleware should is run for HTTP request. (The Sails *

* Router is invoked by the "router" middleware below.) *

*                                                                          *

***************************************************************************/


Order: [

' Startrequesttimer ',

' Cookieparser ',

' Session ',

' Myrequestlogger ',

' Bodyparser ',

' Handlebodyparsererror ',

' Compress ',

' Methodoverride ',

' Poweredby ',

' $custom ',

' Router ',

' www ',

' Favicon ',

' 404 ',

' 500 '

],


/****************************************************************************

*                                                                           *

* Example custom middleware;              Logs each request to the console. *

*                                                                           *

****************************************************************************/


Myrequestlogger:function (req, res, next) {

Console.log ("Requested::", Req.method, Req.url);

return next ();

},



/***************************************************************************

*                                                                          *

* The body parser that would handle incoming multipart HTTP requests. by *

* Default as of v0.10, Sails uses *

* [Skipper] (http://github.com/balderdashy/skipper). See *

* Http://www.senchalabs.org/connect/multipart.html for other options. *

*                                                                          *

***************************************************************************/


Bodyparser:require (' skipper ')


},


/***************************************************************************

*                                                                          *

* The number of seconds to cache flat files on disk being served by *

* Express static middleware (by default, these files is in '. Tmp/public ') *

*                                                                          *

* The HTTP static cache is only active in a ' production ' environment, *

* Since that's the only time Express would cache flat-files. *

*                                                                          *

***************************************************************************/


cache:31557600000

};



Above is the middleware of all routes.

Question: Control a route/A through middleware: [A, B, c], a route/b through middleware: [A, C]



      • Service

SAILSJS Learning Note

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.