从视图中的节点传递的NodeJS值,而不网址
我使用与快递和EJS的NodeJS。
每一个互联网上询问如何从节点传递价值的观点,但什么相反?
例如,我问我的用户输入的字符串的形式,当用户点击该按钮时,我怎么能得到这个字符串,而不把它当作URL中的参数?
表格:
<div class="container">
<form style="width:50%">
<div class="form-group">
<label for="text">Name of the product</label>
<input type="text" class="form-control" id="pName">
</div>
<div class="form-group">
<label for="text">Reciever</label>
<input type="text" class="form-control" id="reciever">
</div>
<div class="form-group">
<label for="text">Location</label>
<input type="text" class="form-control" id="location">
</div>
<!-- <div class="checkbox">
<label><input type="checkbox"> Remember me</label>
</div>-->
<button type="submit" class="btn btn-default">Submit</button>
</form>
在app.js
var express = require('express');
var app = express();
//ALL GLOBAL VARIABLES
var port = 8080;
app.get('/', function(req, res) {
res.render('index.ejs');
});
app.get('/barcode', function(req,res) {
res.render('barcode.ejs');
});
app.listen(port);
我知道我能做到这一点:
app.get('/url/:parameter', function(req.res) {
var foo = req.params.parameter;
}
但是,如果我不希望使用URL,是可以检索数据?
回答如下:使用POST的方法为你的HTML表单
<form action="/myapi" method="post">
<input type="text" class="form-control" id="pName" name="pName">
<button type="submit" class="btn btn-default">Submit</button>
</form>
然后处理在后端的客户端形式的“行动”与app.post
app.post('/myapi', function(req, res) {
res.send('The pName is "' + req.body.pName + '".');
});