2014-01-08 35 views
0

我跑這個代碼,這是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文檔中?

+1

什麼是jQuery專業版? –

+2

有一個很好的機會,你的'cType'不是你所期望的。記錄下來並找出答案。 –

+0

@Jarek指定端口80沒有改變上面的問題不幸 – user2888246

回答

2

這裏有一些提示,讓您的一些結果:

  1. 不要在端口80
  2. 你反應POST聽 - 該瀏覽器將發送GET當你只是在url
  3. 購買一本書的node.js太:-)
  4. 使用這些修改至少拿到了一定的成果:

    var http = require("http"); 
    var querystring = require("querystring"); 
    
    var port = 8888; 
    
    http.createServer(function (req, res) { 
        console.log("[200 OK] " + req.method + " to " + req.url); 
    
        if (req.method == "GET") { 
         var dataObj = new Object(); 
         var cType = req.headers["content-type"]; 
         var fullBody = ""; 
    
    
           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); 
    
  5. 輸入localhost:8888在你的瀏覽器ADRESS場

希望這給你一些想法下一步怎麼辦。

+0

你說得對。我意識到,我試圖打開上述服務器上的htdocs中的html,但是,我只需要打開html(不在本地主機中),並且該網頁發佈了我期待的響應。我打算更多地進入nodejs,因爲它看起來很有趣。感謝mainguy :) @mainguy – user2888246

相關問題