Created
April 6, 2011 23:10
-
-
Save weaver/906731 to your computer and use it in GitHub Desktop.
Revisions
-
weaver revised this gist
Apr 7, 2011 . 5 changed files with 43 additions and 62 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1 @@ node_modules This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,14 +1,14 @@ var Express = require('express'), Form = require('connect-form'), Upload = require('./upload'), app = Express.createServer(); app.use(Express.logger()); app.use(Express.bodyParser()); app.use(Form()); app.use(Express.static(__dirname)); app.post('/upload', Upload.wait, function(req, res) { res.send(req.body, 200); }); This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,58 +0,0 @@ This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,8 @@ { "name": "express-form-example", "main": "./app.js", "dependencies": { "express": ">=1.0.0", "connect-form": ">=0.2.1" } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,30 @@ exports.wait = wait; function wait(req, res, next) { if (!req.form) next(); else req.form.complete(function(err, fields, files) { if (err) next(err); else { req.fields = fields; req.files = files; req.body = extend({}, fields, files); next(); } }); } function extend(target) { var key, obj; for (var i = 1, l = arguments.length; i < l; i++) { if ((obj = arguments[i])) { for (key in obj) target[key] = obj[key]; } } return target; } -
weaver revised this gist
Apr 6, 2011 . 2 changed files with 21 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,16 @@ var Express = require('express'), MultiParser = require('./multiparser'), app = Express.createServer(); app.use(MultiParser()); app.use(Express.logger()); app.use(Express.bodyParser()); app.use(Express.static(__dirname)); app.post('/upload', function(req, res) { console.log('uploaded', req.body); res.send(req.body, 200); }); app.listen(4000); console.log('http://localhost:4000/index.html'); This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,5 @@ <form method="post" action="/upload" enctype="multipart/form-data"> <p>Title: <input type="text" name="title" /></p> <p>File: <input type="file" name="upload" /></p> <p><input type="submit" value="Upload" /></p> </form> -
weaver created this gist
Apr 6, 2011 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,58 @@ // Example middleware for handling multipart forms. It should be // "used" by an app sometime before the bodyDecoder. // // var Express = require('express'); // var MultiParser = require('./multiparser'); // // var app = Express.createServer(); // app.use(MultiParser()); // app.use(Express.bodyDecoder()); var Formidable = require('formidable'); exports = module.exports = function bodyParser(opt) { return function bodyParser(req, res, next) { var parser = exports.parse[mime(req)]; if (parser && !req.body) { parser(opt, req, res, next); } else { next(); } }; }; // Grab the general mime type from a request. function mime(req) { var str = req.headers['content-type'] || ''; return str.split(';')[0]; } function parseMultipart(opt, req, res, next) { var form = new Formidable.IncomingForm(); form.parse(req, function(err, fields, files) { if (err) next(err); else { req.body = extend(fields, files); next(); } }); } function extend(target) { var key, obj; for (var i = 1, l = arguments.length; i < l; i++) { if ((obj = arguments[i])) { for (key in obj) target[key] = obj[key]; } } return target; } exports.parse = { 'multipart/form-data': parseMultipart };