sailsjs learning note

來源:互聯網
上載者:User

標籤:

menu list:

  • custom controller

  • custom 模組使用

  • custom model

  • custom middleware  

  • custom service ?

 

 




  • 路由與對應的controller處理:

    • 用命令列 & controller 變化

wade-mac:fin_server_invest mac$ sails generate controller mail sendmaillog

info: Created a new controller ("mail") at api/controllers/MailController.js!

  • 路由總匯總:config/routes.js

想往常一樣加:, ‘GET /make/a/mail‘:"MailController.sendmaillog"

  • 模組的使用: --  與以前一樣

/**

* 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 that use the               *

 * `customMiddleware` config option.                                         *

 *                                                                           *

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


 middleware: {


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

 *                                                                          *

 * The order in which middleware should be 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 will 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 are in `.tmp/public`) *

 *                                                                          *

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

 * since that‘s the only time Express will cache flat-files.                *

 *                                                                          *

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


 cache: 31557600000

};



上面的是所有的路由都經過的middleware

疑問:控制某個路由/a  經過middleware: [a, b, c ] , 某個路由/b 經過middleware: [a, c ]



    • service

sailsjs learning note

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.