Skip to content

Instantly share code, notes, and snippets.

@mcantelon
Created March 26, 2011 07:05
Show Gist options
  • Save mcantelon/888089 to your computer and use it in GitHub Desktop.
Save mcantelon/888089 to your computer and use it in GitHub Desktop.
connect-form example
var connect = require('connect'),
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();
}
},
connect.router(function(app) {
app.get('/', function(req, res, next) {
res.setHeader("Content-Type", "text/html")
res.write('<form method="post" enctype="multipart/form-data" action="/" />')
res.write('<p>Comment: <input name="comment" type="text" /></p>')
res.write('<p>File: <input name="myfile" type="file" /></p>')
res.end('<input type="submit"></form>')
});
})
);
server.listen(8000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment