Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added bodyParser example

  • Loading branch information...
commit 1141370846d05bfa98dfc6111ef510a9a1cacf5f 1 parent e1de4f8
@tj tj authored
Showing with 30 additions and 0 deletions.
  1. +25 −0 examples/bodyParser.js
  2. +5 −0 examples/public/form.html
View
25 examples/bodyParser.js
@@ -0,0 +1,25 @@
+
+var connect = require('../');
+
+// visit form.html
+
+connect.createServer()
+ .use(connect.static(__dirname + '/public'))
+ .use(connect.bodyParser())
+ .use(function(req, res, next){
+ if ('GET' != req.method) return next();
+ res.statusCode = 302;
+ res.setHeader('Location', 'form.html');
+ res.end();
+ })
+ .use(function(req, res){
+ res.setHeader('Content-Type', 'text/html');
+ res.write('<p>thanks ' + req.body.name + '</p>');
+ res.write('<ul>');
+ req.body.images.forEach(function(image){
+ var kb = image.size / 1024 | 0;
+ res.write('<li>uploaded ' + image.name + ' ' + kb + 'kb</li>');
+ });
+ res.end('</ul>');
+ })
+ .listen(3000);
View
5 examples/public/form.html
@@ -0,0 +1,5 @@
+<form action="/" method="post" enctype="multipart/form-data">
+ <input type="text" name="name" placeholder="Name:" />
+ <input type="file" name="images" multiple="multiple" />
+ <input type="submit" value="Upload" />
+</form>
Please sign in to comment.
Something went wrong with that request. Please try again.