一个简单的httpserver。接收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执行这个文件,然后再浏览器中输出:http://192.168.33.98:8080/?name=xulidong&passward=123456,192.168.33.98是我的电脑ip。
执行结果如图: