Updated multipart example with acceptance test.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Robert Sköld · committed by TJ Holowaychuk · parent a819856 · Browse files · 2 files·+59 added−29 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+59 added−29 removed
- tests:+40 added−0 removed
- other:+19 added−29 removed
Provenance
- Commit
970031e- Tree
c36f3ea- Author
Robert Sköld <robert@publicclass.se>- Committer
TJ Holowaychuk <tj@vision-media.ca>- Parent
a819856
2 files changed+59 added−29 removed
modified:examples/multipart/app.js+19 added-29 removed
12345676789101112131491011121314151617181920212223242526272829303132333435363725262739404142434428293031323334354748363738
/** * Module dependencies. */
var express = require('../../lib/express') , form = require('connect-form');var express = require('../../') , format = require('util').format;
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 }));var app = module.exports = express()
// bodyParser in connect 2.x uses node-formidable to parse // the multipart form data.app.use(express.bodyParser())
app.get('/', function(req, res){ res.send('<form method="post" enctype="multipart/form-data">' + '<p>Title: <input type="text" name="title" /></p>' + '<p>Image: <input type="file" name="image" /></p>' + '<p><input type="submit" value="Upload" /></p>' + '</form>');});
app.post('/', function(req, res, next){
// 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('\nuploaded %s to %s' , files.image.filename , files.image.path); res.redirect('back'); } }); // the uploaded file can be found as `req.files.image` and the // title field as `req.body.title`
// 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'); }); res.send(format('\nuploaded %s (%d Kb) to %s as %s' , req.files.image.name , (req.files.image.size / 1024) << 0 , req.files.image.path , req.body.title));});
if (!module.parent) {app.listen(3000);console.log('Express app started on port 3000'); app.listen(3000); console.log('Express started on port 3000');}No newline at end of fileadded:test/acceptance/multipart.js+40 added
12345678910111213141516171819202122232425262728293031323334353637383940var app = require('../../examples/multipart/app') , fs = require('fs') , request = require('../support/http');
var logo = fs.readFileSync(__dirname+'/../../docs/images/logo.png') , boundary = '------expressmultipart';
describe('multipart', function(){ describe('GET /', function(){ it('should respond with a form', function(done){ request(app) .get('/') .expect(/<form/,done) }) })
describe('POST /', function(){ it('should upload logo as multipart', function(done){ request(app) .post('/') .set('content-type','multipart/form-data; boundary='+boundary.slice(2)) .write(boundary+'\r\n') .write('Content-Disposition: form-data; name="title"\r\n') .write('\r\n') .write('logo\r\n') .write(boundary+'\r\n') .write('Content-Disposition: form-data; name="image"; filename="logo.png"\r\n') .write('Content-Type: image/png\r\n') .write('\r\n') .write(logo+'\r\n') .write(boundary+'--\r\n') .end(function(res){ res.body.should.match(/uploaded logo.png/) res.body.should.match(/\(12 Kb\)/) res.body.should.match(/as logo/) done() }) }) })})No newline at end of file