当前位置: 首页 > 工具软件 > Sails.js > 使用案例 >

sailsjs learning note

岳鸿畴
2023-12-01

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
 类似资料:

相关阅读

相关文章

相关问答