-
-
Save tj/1049900 to your computer and use it in GitHub Desktop.
This file contains 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 characters
// Expose modules in ./support for demo purposes | |
require.paths.unshift(__dirname + '/../../support'); | |
/** | |
* Module dependencies. | |
*/ | |
var express = require('../../lib/express') | |
, form = require('connect-form'); | |
var app = express.createServer( | |
// connect-form (http://github.com/visionmedia/connect-form) | |
// middleware uses the formidable middleware to parse urlencoded | |
// and multipart form data | |
form({ keepExtensions: true }) | |
); | |
app.configure(function(){ | |
app.use(express.bodyParser()); | |
}); | |
app.get('/', function(req, res){ | |
res.send('<form method="post" action="/">' | |
//enctype="multipart/form-data">' | |
// + '<p>Image: <input type="file" name="image" /></p>' | |
+ '<p>Text: <input type="text" name="sometext" /></p>' | |
+ '<p><input type="submit" value="Upload" /></p>' | |
+ '</form>'); | |
}); | |
app.post('/', function(req, res, next){ | |
// console.log(req); | |
if (req.form) { | |
// connect-form adds the req.form object | |
// we can (optionally) define onComplete, passing | |
// the exception (if any) fields parsed, and files parsed | |
req.form.complete(function(err, fields, files){ | |
if (err) { | |
next(err); | |
} else { | |
console.log(req.fields); | |
console.log(req.files); | |
// console.log('\nuploaded %s to %s' | |
// , files.image.filename | |
// , files.image.path); | |
res.redirect('back'); | |
} | |
}); | |
} else { | |
console.log('not formidable'); | |
console.log(req.body); | |
} | |
// We can add listeners for several form | |
// events such as "progress" | |
// req.form.on('progress', function(bytesReceived, bytesExpected){ | |
// var percent = (bytesReceived / bytesExpected * 100) | 0; | |
// process.stdout.write('Uploading: %' + percent + '\r'); | |
// }); | |
}); | |
app.listen(3000); | |
console.log('Express app started on port 3000'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment