Skip to content
Browse files

Added lib/connect-form.js

This will help with things like nDistro which
utilize lib/*
  • Loading branch information...
1 parent 088d048 commit f69596d02c9fdaff0ab2f10d0f5214be960f91b5 @tj tj committed Jul 27, 2010
Showing with 84 additions and 83 deletions.
  1. +0 −83 index.js
  2. +1 −0 index.js
  3. +83 −0 lib/connect-form.js
View
83 index.js
@@ -1,83 +0,0 @@
-
-/*!
- * Connect - Multipart
- * Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>
- * MIT Licensed
- */
-
-/**
- * Module dependencies.
- */
-
-var utils = require('connect/utils'),
- formidable = require('formidable');
-
-/**
- * Setup form with the given `options`.
- *
- * Options:
- *
- * - `encoding` Encoding used for incoming forms. Defaults to utf8
- * - `uploadDir` Directory to save uploads. Defaults to "/tmp"
- * - `keepExtensions` Include original extensions. Defaults to `false`
- *
- * Examples:
- *
- * var form = require('connect-form');
- * var server = connect.createServer(
- * form({ keepExtensions: true }),
- * function(req, res, next){
- * // Form was submitted
- * if (req.form) {
- * // Do something when parsing is finished
- * // and respond, or respond immediately
- * // and work with the files.
- * req.form.complete(function(err, fields, files){
- * res.writeHead(200, {});
- * if (err) res.write(JSON.stringify(err.message));
- * res.write(JSON.stringify(fields));
- * res.write(JSON.stringify(files));
- * res.end();
- * });
- * // Regular request, pass to next middleware
- * } else {
- * next();
- * }
- * }
- * );
- *
- * @param {Object} options
- * @return {Function}
- * @api public
- */
-
-module.exports = function(options){
- options = options || {};
- return function(req, res, next){
- if (formRequest(req)) {
- var callback = function(){},
- form = req.form = new formidable.IncomingForm;
- utils.merge(form, options);
- form.complete = function(fn){ callback = fn; };
- form.parse(req, function(){
- callback.apply(this, arguments);
- });
- }
- next();
- };
-};
-
-/**
- * Check if `req` is a valid form request.
- *
- * @param {IncomingMessage} req
- * @return {Boolean}
- * @api private
- */
-
-function formRequest(req){
- return (req.method === 'POST'
- || req.method === 'PUT')
- && (req.headers['content-type'].indexOf('multipart/form-data') >= 0
- || req.headers['content-type'].indexOf('urlencoded') >= 0);
-}
View
1 index.js
View
83 lib/connect-form.js
@@ -0,0 +1,83 @@
+
+/*!
+ * Connect - Multipart
+ * Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>
+ * MIT Licensed
+ */
+
+/**
+ * Module dependencies.
+ */
+
+var utils = require('connect/utils'),
+ formidable = require('formidable');
+
+/**
+ * Setup form with the given `options`.
+ *
+ * Options:
+ *
+ * - `encoding` Encoding used for incoming forms. Defaults to utf8
+ * - `uploadDir` Directory to save uploads. Defaults to "/tmp"
+ * - `keepExtensions` Include original extensions. Defaults to `false`
+ *
+ * Examples:
+ *
+ * var form = require('connect-form');
+ * var server = connect.createServer(
+ * form({ keepExtensions: true }),
+ * function(req, res, next){
+ * // Form was submitted
+ * if (req.form) {
+ * // Do something when parsing is finished
+ * // and respond, or respond immediately
+ * // and work with the files.
+ * req.form.complete(function(err, fields, files){
+ * res.writeHead(200, {});
+ * if (err) res.write(JSON.stringify(err.message));
+ * res.write(JSON.stringify(fields));
+ * res.write(JSON.stringify(files));
+ * res.end();
+ * });
+ * // Regular request, pass to next middleware
+ * } else {
+ * next();
+ * }
+ * }
+ * );
+ *
+ * @param {Object} options
+ * @return {Function}
+ * @api public
+ */
+
+module.exports = function(options){
+ options = options || {};
+ return function(req, res, next){
+ if (formRequest(req)) {
+ var callback = function(){},
+ form = req.form = new formidable.IncomingForm;
+ utils.merge(form, options);
+ form.complete = function(fn){ callback = fn; };
+ form.parse(req, function(){
+ callback.apply(this, arguments);
+ });
+ }
+ next();
+ };
+};
+
+/**
+ * Check if `req` is a valid form request.
+ *
+ * @param {IncomingMessage} req
+ * @return {Boolean}
+ * @api private
+ */
+
+function formRequest(req){
+ return (req.method === 'POST'
+ || req.method === 'PUT')
+ && (req.headers['content-type'].indexOf('multipart/form-data') >= 0
+ || req.headers['content-type'].indexOf('urlencoded') >= 0);
+}

0 comments on commit f69596d

Please sign in to comment.
Something went wrong with that request. Please try again.