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.jsfile),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-formidabletoofor uploading files. Confused whether touse
node-formidableor
connect-form?Let
all confusion be cleared, for
connect-formisnothing but
node-formidableitself!Yes,
connect-formisjust
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-formmodule,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
uploadDirparameteris used to specify where we want the files to be uploaded to. Formore details about
connect-form,visit
the
connect-formGitHubpage.
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
methodisset to
postand
enctypeto
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
fieldsobjectof the
req.form.completecallbackobject,
and all the files in the
filesobject.Since you have the references to the POST objects now, process themwhatever way suits you.
相关文章推荐
- JS解析URL参数,并将参数以FORM表单形式提交
- 解析:使用easyui的form提交表单,在IE下出现类似附件下载时提示是否保存的现象
- form表单提交中文乱码的详细解析
- form表单提交中文乱码的详细解析
- form表单提交中文乱码的详细解析
- form 表单提交 拼接url发送到后台
- 解析:使用easyui的form提交表单,在IE下出现类似附件下载时提示是否保存的现象
- 简单解决 multipart/form-data 文件上传表单中 传递参数无法获取的办法! (解决办法:动态更改表单提交URL)
- struts1.x 中提交form[]表单数组以及后台解析
- form表单采用post提交方式提交,在url后面加问号再传递一个参数,提交后是否能接收到?
- js的form表单提交url传参数(包含+等特殊字符)的解决方法
- java利用url解析网页内容并模拟手动form提交数据
- iframe标签实现form表单提交无页面刷新(不需要js)---解析
- form表单提交中文乱码的详细解析
- httpurlconnection模拟post提交form表单(普通文本和上传文件) (
- HttpURLConnection模拟form表单提交文件
- 解析:使用easyui的form提交表单,在IE下出现类似附件下载时提示是否保存的现象
- iframe标签实现form表单提交无页面刷新(不需要js)---解析
- 解决multipart/form-data表单中传递参数问题(动态更改表单提交URL)
- iframe标签实现form表单提交无页面刷新(不需要js)---解析