nodejs 获取url字段
这个demo的原理:
使用java用get方式向nodejs服务端发送数据,nodejs服务器获取请求的url字段,
并打印出来,代码如下:
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);
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");
con = (HttpURLConnection)url.openConnection();
con.setRequestMethod("GET");
con.connect();
con.getInputStream();
} catch (Exception e) {
e.printStackTrace();
}
}
}
运行:首先运行nodejs服务器端代码,然后执行java代码,运行结果:
参考网址:http://nodejs.org/api/url.html#url_url_format_urlobj