您的位置:首页 > 其它

form表单的提交和对URL的解析

2013-01-04 21:33 211 查看


原文地址:http://www.hacksparrow.com/form-handling-processing-in-express-js.html


Handle / Process GET requests in Express

There are two ways of passing parameters to an Express application:i. Using named placeholders, ii. GET parameters.

i. NamedPlaceholders

Named placeholders are defined in Express routes (very likely inthe
app.js
file),and use URL fragments for relaying information to the app. A namedplaceholder might
be defined and accessed this way in Express:
app.get('/user/:id', function(req, res) {

res.send('user ' + req.params.id);

});


ii. GETParameters

Another method of sending information to an app in the URL is topass them via GET parameters.

If this is the URL
http://localhost/books?category=biographies&type=paperback


you access the GET parameters in Express, this way:
console.log('Category: ' + req.query['category']);

console.log('Type: ' + req.query['type']);



Handle / Process POST requests inExpress

The POST request method also has two aspects to it: i. text data,ii. file uploads. Let's see how they are handled in Express.

i. Text Data

All default Express applications are ready to handle text POSTrequests using the bodyParsermiddleware.Look for
app.use(express.bodyParser())
in
app.js
,if
you see that, you are good to go.

If your form looks like this:
<form method="post">

<input type="text" name="username">

<input type="password" name="password">

<input type="submit">

</form>


you can access the POST submitted text data this way:
console.log('Username: ' + req.body.username);

console.log('Password: ' + req.body.password);


ii. File Uploads

This part of the article is not recommended if you are working withthe latest version of Express. Read thisinstead.

File uploads in Express is not that straightforward as of thiswriting. But it isn't that complicated either, you just need to doa few things and you'll be all set up to handle file uploadstoo.

First of all you'll need to install
connect-form
.
$ npm install connect-form

connect-form@0.2.1 ./node_modules/connect-form

└── formidable@1.0.8


You might have read/seen somewhere that you canuse
node-formidable
toofor uploading files. Confused whether touse
node-formidable
or
connect-form
?Let
all confusion be cleared, for
connect-form
isnothing but
node-formidable
itself!Yes,
connect-form
isjust
an Express-friendly wrapper on topof
node-formidable
.

After you have installed
connect-form
,you need to make some changes to
app.js
.Require
the
connect-form
module,and pass a form object to
express.createServer
.
var form = require('connect-form');

var app = module.exports = express.createServer(form({ keepExtensions: true, uploadDir:'./uploads' }));


The
uploadDir
parameteris used to specify where we want the files to be uploaded to. Formore details about
connect-form
,visit
the
connect-form
GitHubpage
.

So here is the updated HTML form:
<form method="post" enctype="multipart/form-data" action="/file-upload">

<input type="text" name="username">

<input type="password" name="password">

<input type="file" name="thumbnail">

<input type="submit">

</form>


Make sure
method
isset to
post
and
enctype
to
multipart/form-data
,else
file uploads just won't work. It is very important to notethat once you have set your form to
multipart/form-data
,you will no longer be able to access the post datafrom
req.body
!

Here is the Express route and an example code to handle the aboveform:
app.post('/file-upload', function(req, res, next) {

req.form.complete(function(err, fields, files) {

if (err) { next(err); }

else {

console.log(fields);

console.log('---------------');

console.log(files);

res.redirect(req.url);

}

});

});


All the POST text fields and values are available inthe
fields
objectof the
req.form.complete
callbackobject,
and all the files in the
files
object.Since you have the references to the POST objects now, process themwhatever way suits you.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: