一个简单的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("<h1>objectUrl</h1>"); for (var i in objectUrl) { if (typeof (objectUrl[i]) != "function") response.write(i + ":" + objectUrl[i] + "<br>"); } response.write("<h1>objectQuery</h1>"); for (var i in objectQuery) { response.write(i + ":" + objectQuery[i] + "<br>"); } response.end(); }).listen(8080, '192.168.33.98');
使用node运行这个文件,然后再浏览器中输出:http://192.168.33.98:8080/?name=xulidong&passward=123456,192.168.33.98是我的电脑ip。
运行结果如图:
时间: 2024-10-13 16:14:48