我跑這個代碼,這是jQuery的親書2.0:使用命令行node.exe formserver.js 這將返回「端口準備Node.js的運行在cmd中而不是在瀏覽器
var http = require("http");
var querystring = require("querystring");
var port = 80;
http.createServer(function (req, res) {
console.log("[200 OK] " + req.method + " to " + req.url);
if (req.method == "POST") {
var dataObj = new Object();
var cType = req.headers["content-type"];
var fullBody = "";
if (cType && cType.indexOf("application/x-www-form-urlencoded") > -1) {
req.on("data", function(chunk) { fullBody += chunk.toString();});
req.on("end", function() {
res.writeHead(200, "OK", {"Content-Type": "text/html"});
res.write("<html><head><title>Post data</title></head><body>");
res.write("<style>th, td {text-align:left; padding:5px; color:black}\n");
res.write("th {background-color:grey; color:white; min-width:10em}\n");
res.write("td {background-color:lightgrey}\n");
res.write("caption {font-weight:bold}</style>");
res.write("<table border='1'><caption>Form Data</caption>");
res.write("<tr><th>Name</th><th>Value</th>");
var dBody = querystring.parse(fullBody);
for (var prop in dBody) {
res.write("<tr><td>" + prop + "</td><td>"
+ dBody[prop] + "</td></tr>");
}
res.write("</table></body></html>");
res.end();
});
}
}
}).listen(port);
console.log("Ready on port " + port);
90' 。 每當我撥打電話爲localhost:90在我的瀏覽器,它註冊在cmd中要求爲「200 OK GET到/」
在它說,大約一分鐘「等待本地主機」的瀏覽器。然後它以'沒有數據收到消息'失敗。
在這本書中,作者正在使用www。擴展服務他的網頁,但是,我正在使用'localhost'像大多數普通初學者一樣
我有兩個問題,我如何獲取本地主機加載和我在哪裏保存我的html文件,以便本地主機將看到他們。它是否在保存上述代碼的相同文件夾中?或者在nodejs安裝的html文檔中?
什麼是jQuery專業版? –
有一個很好的機會,你的'cType'不是你所期望的。記錄下來並找出答案。 –
@Jarek指定端口80沒有改變上面的問題不幸 – user2888246