{"id":7984,"date":"2015-11-15T02:15:16","date_gmt":"2015-11-15T02:15:16","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2015\/11\/15\/adding-custom-express-middleware-like-jquery-file-upload-to-sails-js-open-source-projects-blueimp-jquery-file-upload\/"},"modified":"2015-11-15T02:15:16","modified_gmt":"2015-11-15T02:15:16","slug":"adding-custom-express-middleware-like-jquery-file-upload-to-sails-js-open-source-projects-blueimp-jquery-file-upload","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2015\/11\/15\/adding-custom-express-middleware-like-jquery-file-upload-to-sails-js-open-source-projects-blueimp-jquery-file-upload\/","title":{"rendered":"Adding custom Express middleware like jQuery-File-Upload to Sails.js-open source projects blueimp\/jQuery-File-Upload"},"content":{"rendered":"<p>In the case of sails.js and jQuery File upload i think, you can replace sails bodyParser to jQuery file uploader post method, idea from this thread:<\/p>\n<p>nginx \/ sails.js: incomplete file upload<\/p>\n<p>below example works for me fine. <strong>sails js 0.10.5<\/strong><\/p>\n<pre><code>npm install blueimp-file-upload-expressjs --save\nnpm install lodash --save\n<\/code><\/pre>\n<p>uncomment and add lines in the file <strong>\/config\/http.js<\/strong>:<\/p>\n<pre><code>middleware: {\n\n       cbodyParser: require('..\/cbodyParser')( { urls: [\/\\\/uploads\/]} ),\n\n    order: [\n       'startRequestTimer',\n       'cookieParser',\n       'session',\n       'myRequestLogger',\n\n       'cbodyParser',       \/\/ intersept multipart requests\n       'bodyParser',\n\n       'handleBodyParserError',\n       'compress',\n       'methodOverride',\n       'poweredBy',\n       '$custom',\n       'router',\n       'www',\n       'favicon',\n       '404',\n       '500'\n    ]\n}\n<\/code><\/pre>\n<p>new file in root folder <strong>\/cbodyParser.js<\/strong>:<\/p>\n<pre><code>var _ = require('lodash');\n\nvar options = {\n    tmpDir:  __dirname + '\/uploads\/tmp',\n    publicDir: __dirname + '\/uploads',\n    uploadDir: __dirname + '\/uploads',\n    uploadUrl:  '\/uploads\/',\n    maxPostSize: 11000000000, \/\/ 11 GB\n    minFileSize:  1,\n    maxFileSize:  10000000000, \/\/ 10 GB\n    acceptFileTypes:  \/.+\/i,\n    inlineFileTypes:  \/\\.(gif|jpe?g|png)$\/i,\n    imageTypes:  \/\\.(gif|jpe?g|png)$\/i,\n    imageVersions: {\n        width:  220,\n        height: 220\n    },\n    accessControl: {\n        allowOrigin: '*',\n        allowMethods: 'POST',\n        allowHeaders: 'Content-Type, Content-Range, Content-Disposition'\n    },\n    nodeStatic: {\n        cache:  3600 \/\/ seconds to cache served files\n    }\n};\n\nvar uploader = require('blueimp-file-upload-expressjs')(options);\n\nfunction mime(req) {\n    var str = req.headers['content-type'] || '';\n    return str.split(';')[0];\n}\n\n\/\/ start jQuery file uploader here:\nfunction parseMultipart(req, res, next) {\n\n    uploader.post(req, res, function (obj) {\n        res.send(JSON.stringify(obj));\n    });\n\n    next();\n\n}\n\n\/\/ check for post method in request\nfunction disable_parser(opts, req, res)  {\n    var matched = false;\n    try {\n        var method = null;\n        try {method = req.method.toLowerCase();}\n        catch(err){ \/* *\/}\n        if(method) {\n            _(opts.urls).forEach(function(turl) {\n                if (method === 'post' &amp;&amp; req.url.match(turl)) {\n                    \/\/ console.log(\"matched\"+ req.url);\n                    if(!matched) matched = true;\n                };});\n        }\n    } catch(err) { debug(err);\/* pass *\/ }\n    return matched;\n}\n\n\n\/\/ Start all stuff..\nmodule.exports = function toParseHTTPBody(options) {\n    options = options || {};\n\n    \/\/ NAME of anynonymous func IS IMPORTANT (same as the middleware in config) !!!\n    return function cbodyParser(req, res, next) {\n        if (disable_parser(options, req, res) &amp;&amp; mime(req) == 'multipart\/form-data') {  \n            \/\/ we found multipart request to \/uploads, so we can use jQuery file uploader instead\n            return parseMultipart(req, res, next);    \n        } else {\n            return next();\n        }\n    };\n};\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In the case of sails.js and jQuery File upload i think, you can replace sails bodyParser to jQuery file uploader post method, idea from this thread: nginx \/ sails.js: incomplete file upload below example works for me fine. sails js 0.10.5 npm install blueimp-file-upload-expressjs &#8211;save npm install lodash &#8211;save uncomment and add lines in the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7984","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7984","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=7984"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7984\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=7984"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=7984"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=7984"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}