一个简单的http服务器,接收get请求,并返回解析之后的数据。

下面是服务的代码:

 

var http = require("http");
var url = require("url");
var querystring = require("querystring");
 
http.createServer(function (request, response) {
	var objectUrl = url.parse(request.url);
	var objectQuery = querystring.parse(objectUrl.query);
 
	response.writeHead(200, {"content-type": "text/html"});
 
	response.write("

objectUrl

"); for (var i in objectUrl) { if (typeof (objectUrl[i]) != "function") response.write(i + ":" + objectUrl[i] + "
"); } response.write("

objectQuery

"); for (var i in objectQuery) { response.write(i + ":" + objectQuery[i] + "
"); } response.end(); }).listen(8080, '192.168.33.98');

 

使用node运行这个文件,然后再中输出:www.2cto.com,192.168.33.98是我的电脑ip。

运行结果如图:

\