Skip to content
Browse files

tweak bodyParser example

  • Loading branch information...
1 parent 4adbf79 commit 5ddbef6081bbfc2d915d5372a19a4dd6227c3883 @tj tj committed
Showing with 25 additions and 21 deletions.
  1. +25 −21 examples/bodyParser.js
View
46 examples/bodyParser.js
@@ -7,30 +7,34 @@ var connect = require('../')
var app = connect()
.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>');
-
- if (Array.isArray(req.files.images)) {
- req.files.images.forEach(function(image){
- var kb = image.size / 1024 | 0;
- res.write('<li>uploaded ' + image.name + ' ' + kb + 'kb</li>');
- });
- } else {
- var image = req.files.images;
+ .use(form)
+ .use(upload);
+
+function form(req, res, next){
+ if ('GET' != req.method) return next();
+ res.statusCode = 302;
+ res.setHeader('Location', 'form.html');
+ res.end();
+}
+
+function upload(req, res){
+ res.setHeader('Content-Type', 'text/html');
+ res.write('<p>thanks ' + req.body.name + '</p>');
+ res.write('<ul>');
+
+ if (Array.isArray(req.files.images)) {
+ req.files.images.forEach(function(image){
var kb = image.size / 1024 | 0;
res.write('<li>uploaded ' + image.name + ' ' + kb + 'kb</li>');
- }
+ });
+ } else {
+ var image = req.files.images;
+ var kb = image.size / 1024 | 0;
+ res.write('<li>uploaded ' + image.name + ' ' + kb + 'kb</li>');
+ }
- res.end('</ul>');
- });
+ res.end('</ul>');
+}
http.Server(app).listen(3000);
console.log('Server started on port 3000');

0 comments on commit 5ddbef6

Please sign in to comment.
Something went wrong with that request. Please try again.