nodejs 获取get中携带的参数值
demo功能:
通过java端get请求向nodejs端发送数据,nodejs接收数据,并将其值打印出来:
nodejs端:
var http = require("http");
var url = require("url");
var data;
http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
data = url.parse(request.url);
console.log(data);
console.log("----------------------------");
data = url.format(request.url);
console.log(data);
console.log("----------------------------");
//获取get中携带的参数
data = url.parse(request.url,true).query.name + "&" + url.parse(request.url,true).query.age + "&" + url.parse(request.url,true).query.sex;
console.log(data);
response.end();
}).listen(8888);
java端:
public class NodejsSendData {
public static void main(String[] args) {
URL url;
HttpURLConnection con = null;
try {
url = new URL("http://localhost:8888/" + "?name=javaGet&age=22&sex=male");
con = (HttpURLConnection)url.openConnection();
con.setRequestMethod("GET");
con.connect();
con.getInputStream();
} catch (Exception e) {
e.printStackTrace();
}
}
}
运行结果: