如何在Node.js上获得Express.js中的get多个变量
How to get GET multiple variables in Express.js on Node.js?
我正试图从html页面到Node Js服务器获取表单数据。
我的html文件是index.html
<body>
<nav>
<ul>
<li>
<a href="#" class="button add">Add Product</a>
<div class="dialog" style="display:none">
<div class="title">Add Product</div>
<form action="" method="get">
<input id = "name" name="name" type="text" placeholder="Product Name"/>
<input name="code" type="text" placeholder="Product Code"/>
<input name="category" type="text" placeholder=" Category"/>
<input name="brand" type="text" placeholder="Brand"/>
<input type="submit" value="Ok"/>
</form>
</div>
</li>
<li class="radio">
<a href="#" class="button active"></a>
<a href="#" class="button"></a>
<a href="#" class="button"></a>
</li>
</ul>
</div>
</nav>
<p></p>
<script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
<script src="js/index.js"></script>
</body>
提交表单后,服务器没有执行任何操作,只是获取包含所有详细信息的url
server.jsvar express = require("express"),
app = express(),
bodyParser = require('body-parser'),
errorHandler = require('errorhandler'),
methodOverride = require('method-override'),
hostname = process.env.HOSTNAME || 'localhost',
port = parseInt(process.env.PORT, 10) || 4004,
publicDir = process.argv[2] || __dirname + '/public';
var exec = require('child_process').exec;
var fs = require('fs');
//Show homepage
app.get("/", function (req, res) {
res.redirect("/index.html");
console.log("shubh ");
});
app.get("/index/", function (req, res) {
res.redirect("/index.html");
console.log("shubham ");
});
app.get("/index/:name", function (req, res){
console.log("shubham batra");
var keyword = req.query.code;
console.log(keyword);
//res.send('You sent the name "' + req.body.name + '".');
console.log(res.body);
});
app.use(errorHandler({
dumpExceptions: true,
showStack: true
}));
//Search page
app.use(methodOverride());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(express.static(publicDir));
app.use(errorHandler({
dumpExceptions: true,
showStack: true
}));
console.log("Server showing %s listening at http://%s:%s", publicDir, hostname, port);
app.listen(port);
您的表单操作是",因此表单创建的URL将是/name = xxx&代码= yyy等
从你的路由,似乎你希望url的形式是/xxx?code=yyy。
让你的表单动作'/search',并添加以下JS:
app.get("/search", function (req, res){
console.log("shubham batra");
var keyword = req.query.code;
console.log(keyword);
//res.send('You sent the name "' + req.query.name + '".');
});
当你发出get请求时,请求。
您可以使用任意多个,例如:
var code = req.query.code;
var category = req.query.category;
var brand = req.query.brand;
如果是GET请求,则为req.query
;如果是POST请求,则为req.body
。
改变app.get("/index/:name", function (req, res){
to app.get("/index/:name?", function (req, res){
你的路由将匹配/index/和/index/anyname路由
并将窗体动作更改为"/"
或"/action"
相关文章:
- 有没有一种方法可以防止img get请求使用css或js发生
- 如何使用php文件中的GET来获取我在.js文件中声明的变量
- Babel编译错误:找不到模块核心js/library/fn/get迭代器
- Angular js http get返回所有页面
- 如何在nightwatch.js中发送ajax get请求
- 为什么我的get-requesttoserver.js返回离子云9上的实际文件
- Angular.js-$http.get-使用文件中的缓存
- Angular JS `$http`发送POST而不是GET
- JS get元素之间的差异's属性与elem.getAttribute(attr)和elem[attr]
- Can't get the tail to wag (animate) in three.js?
- Strava v3 api JS GET data
- JS $.get Success rather then $.ajax
- Facebook SDK没有;t负载..all.js GET失败
- Node Js.get()需要回调函数
- 下载js GET请求返回的带有唯一密钥的文件
- 使用express和node.js.GET重定向301未被调用
- Node.js get请求问题(异步问题?)
- 用主干JS .get模型事件连接元素索引
- JS Get调用相互等待
- d3.js get JSON from url