Skip to content

Instantly share code, notes, and snippets.

@mikermcneil
Last active May 17, 2021 09:08
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikermcneil/8249181 to your computer and use it in GitHub Desktop.
Save mikermcneil/8249181 to your computer and use it in GitHub Desktop.

File uploads in Sails/Express are changing

Why?

Why is the Connect bodyParser still in Sails/Express?

Sails continues to support bodyParser because it's in the current stable version of Express/Connect. Currently, the plan is to attack the problem in a conventional, baked-in way in the v0.11.0 release of Sails. See http://github.com/mikermcneil/file-parser (streams2 branch is the latest)

Yeah, yeah-- I know what I'm doing. How do I disable that loud deprecation message?

If you already know about all this, and just want to hide the deprecation message from Connect, you can take advantage of this little hack we added:

// config/express.js
module.exports.express = {
  silenceMultipartWarning: true
};

How do I override the bodyParser in Sails?

Open config/express.js

/**
 * Configure options for the Express server inside of Sails.
 *
 * For more information on configuration, check out:
 * http://sailsjs.org/#documentation
 */
module.exports.express = {


	// Configures the middleware function used for parsing the HTTP request body
	// Defaults to the Formidable-based version built-in to Express/Connect
	//
	// To enable streaming file uploads (to disk or somewhere else)
	// you'll want to set this option to `false` to disable the body parser.
	//
	// Defaults to `express.bodyParser`
	//
	// Alternatively, if you're comfortable with the bleeding edge,
	// check out: https://github.com/mikermcneil/stream-debug
	//
	// Example override
	// (for now-- still has the same problems since it's using the core Connect bodyParser)
	bodyParser: function () {
	
		  // Uploader with NO FILE LIMITS
		  var rawUploaderMiddleware = require('express').bodyParser();
		  
		  // Uploader which limits file uploads to files of a certain size
		  var limitedUploaderMiddleware = require('express').bodyParser({
		    limit: 8248242
		  });
		  
		  // Safe uploader (which defers stream processing of the HTTP body to your controllers/policies)
		  var safeUploaderMiddleware = require('express').bodyParser({
		    defer: true
		  });
		  
		  // e.g. if you want to use safe uploads globally
		  // (doesn't use tmp directory)
		  // use the safe/deferred uploader (see the links above for more info on how to do this.
		  // return safeUploaderMiddleware;
		  
		  
		  // e.g. if you want .tmp files with no file size limits, return:
		  // return rawUploaderMiddleware;
		  
		  // e.g. if you want a global limit, you can just return the limited version here:
		  // (still USES TMP FILES!!!)
		  // return limitedUploaderMiddleware;
		  
		  // e.g. if you want a per-route limit, configure that logic here:
		  return function (req, res, next) {
		  	
		  	// If the route looks like a request you want to allow unbridled access to,
		  	// call the raw body parser, or whatever you want, etc.
		  	if (req.method === 'get' && req.url.match(/\/admin/avatar/))) {
		  		rawBodyParserMiddleware(req,res,next);
		  		return;
		  	}
		  	// Otherwise, run the limited (or better yet, the safe/deferred) bodyParser
		  	else {
		  		// limitedUploaderMiddleware(req,res,next);
		  		// or
		  		safeUploaderMiddleware;
		  		return;
		  	}
		  }
	}





	// If bodyParser doesn't understand the HTTP body request data, 
	// run it again with an artificial header, forcing it to try and parse
	// the request body as JSON.
	// (this allows JSON to be used as your request data without the need to 
	// specify a 'Content-type: application/json' header)
	//
	// Defaults to `true`.
	//
	// NOTE: If using the `file-parser` above, you'll want to explicitly disable this:
	// retryBodyParserWithJSON: false,



	// Cookie parser middleware to use
	//			(or false to disable)
	//
	// Defaults to `express.cookieParser`
	//
	// Example override:
	// cookieParser: (function customMethodOverride (req, res, next) {})(),



	// HTTP method override middleware
	//			(or false to disable)
	//
	// This option allows artificial query params to be passed to trick 
	// Express into thinking a different HTTP verb was used.
	// Useful when supporting an API for user-agents which don't allow 
	// PUT or DELETE requests
	//
	// Defaults to `express.methodOverride`
	//
	// Example override:
	// methodOverride: (function customMethodOverride (req, res, next) {})()
};





/**
 * HTTP Flat-File Cache
 * 
 * These settings are for Express' static middleware- stuff like your
 * images, css, etc. 
 *
 * In Sails, this is probably your app's `assets directory.
 * By default, Sails uses your project's Gruntfile to compile/copy those 
 * assets to `.tmp/public`, where they're accessible to Express.
 *
 * The HTTP static cache is only active in a 'production' environment, 
 * since that's the only time Express will cache flat-files.
 *
 * For more information on configuration, check out:
 * http://sailsjs.org/#documentation
 */
module.exports.cache = {

	// The number of seconds to cache files being served from disk
	// (only works in production mode)
	maxAge: 31557600000
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment