将自定义Express中间件(如jQuery-File-Upload)添加到Sails.js [英] Adding custom Express middleware like jQuery-File-Upload to Sails.js

查看:139
本文介绍了将自定义Express中间件(如jQuery-File-Upload)添加到Sails.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍然很难理解如何在sails.js中添加中间件.我听说过使用policy.js,创建自定义策略,添加到local.js等.所以有人可以告诉我如何精确添加

I'm still having difficulty understanding how to add middleware to sails.js. I've heard use policies.js, create custom policies, add to local.js, etc. So could someone please show me how exactly I would add the jquery-file-upload-middleware to a sails app. Thanks in advance

推荐答案

在以前的Sails版本中,这将非常困难,因为您无法控制包含自定义中间件的顺序.在v0.10中,这很困难.

This would have been very difficult in previous versions of Sails, because you had no control over the order in which custom middleware was included. In v0.10, it's just kinda difficult.

注意:以下内容适用于 Sails的Beta版本(v0.10.x),可安装通过npm install sails@beta.

Note: the following will work with the beta version of Sails (v0.10.x), installable via npm install sails@beta.

将自己的自定义中间件插入Sails就像将customMiddleware函数添加到以app作为参数的config/express.js文件中一样容易;然后,您可以app.use保持内心的满足.这种方法的缺点是,它不会让您控制何时包括中间件.值得注意的是,它在正文解析器的之后中包含,这不适用于您的情况.

Inserting your own custom middleware into Sails is as easy as adding a customMiddleware function to your config/express.js file which takes app as an argument; you can then app.use to your heart's content. The downside with this approach is that it doesn't give you control over when your middleware is included. Notably, it's included after the body parser, which won't work for your case.

在最新版本的Sails中,您可以通过在/config/express.js中实现loadMiddleware方法来覆盖中间件加载的 all .参数是appdefaultMiddleware(默认情况下Sails通常包括的一组中间件功能)和sails(对全局Sails对象的引用).首先看看默认核心实现可能要复制相同的订单.因此,在您的/config/express.js中,您将拥有类似的东西:

In the newest version of Sails, you can override all of the middleware loading by implementing a loadMiddleware method in /config/express.js. The arguments are app, defaultMiddleware (the set of middleware functions that Sails usually includes by default), and sails (a reference to the global Sails object). Take a look at the default core implementation first--you'll probably want to copy the same order. So in your /config/express.js, you'd have something like:

var upload = require('jquery-file-upload-middleware');

// configure upload middleware
upload.configure({
    uploadDir: __dirname + '/public/uploads',
    uploadUrl: '/uploads',
    imageVersions: {
        thumbnail: {
            width: 80,
            height: 80
        }
    }
});

module.exports.express = {

    loadMiddleware: function(app, defaultMiddleware, sails) {

        // Use the middleware in the correct order
        app.use(defaultMiddleware.startRequestTimer);
        app.use(defaultMiddleware.cookieParser);
        app.use(defaultMiddleware.session);
        // Insert upload file handler
        app.use('/upload', upload.fileHandler());
        app.use(defaultMiddleware.bodyParser);
        app.use(defaultMiddleware.handleBodyParserError);
        app.use(defaultMiddleware.methodOverride);
        app.use(defaultMiddleware.poweredBy);
        app.use(defaultMiddleware.router);
        app.use(defaultMiddleware.www);
        app.use(defaultMiddleware.favicon);
        app.use(defaultMiddleware[404]);
        app.use(defaultMiddleware[500]);
    }

    ...etc...

}

这篇关于将自定义Express中间件(如jQuery-File-Upload)添加到Sails.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆