簡體   English   中英

我應該如何理解和使用用Node.js編寫的服務器?

[英]How shall I understand and use this server written in Node.js?

我正在嘗試了解用node.js編寫的簡單服務器

var http = require('http'); 
var querystring = require('querystring'); 

http.createServer(function (req, res) { 
  switch(req.url) { 
    case '/form': 
        if (req.method == 'POST') { 
         console.log("[200] " + req.method + " to " + req.url); 
         var fullBody = ''; 
         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(); 
         }); 
       } else { 
         console.log("[405] " + req.method + " to " + req.url); 
         res.writeHead(405, "Method not supported", {'Content-Type': 'text/html'}); 
         res.end('<html><head><title>405 - Method not supported</title></head><body>' + 
                 '<h1>Method not supported.</h1></body></html>'); 
       } 
      break; 
    default: 
      res.writeHead(404, "Not found", {'Content-Type': 'text/html'}); 
      res.end('<html><head><title>404 - Not found</title></head><body>' + 
              '<h1>Not found.</h1></body></html>'); 
      console.log("[404] " + req.method + " to " + req.url); 
  }; 
}).listen(8080); 

函數req.on()什么作用? 例如req.on('data',...)req.on('end',...)嗎? https://nodejs.org/api/http.html中的某處進行了解釋嗎? 我想我可能已經錯過了。

如何將HTTP請求發送到服務器,以使case '/form': if (req.method == 'POST'){...}中的部分得以執行case '/form': if (req.method == 'POST'){...} 具體來說,在使用curl ,應為curl提供哪些參數和選項? 如果使用Firefox瀏覽器怎么辦?

Req.on('data')意味着您的服務器正在從客戶端接收數據,在附加到req.on('data')的回調中,您通常將數據連接起來,然后解析數據以供以后使用。 一旦接收到全部數據,將執行附加到req.on('end')的回調,在這里您可以根據接收到的數據執行所有業務邏輯,然后將響應發送回客戶端

現在如何訪問/ form URL?

通過POST或PUT請求發送數據時,兩種常見格式(通過Content-Type標頭指定)是:

  1. 應用程序/ JSON
  2. 應用程序/ x-WWW窗體-urlencoded

您可以使用郵遞員客戶端或curl來訪問/ form URL。

卷發

curl -H "Content-Type:application/x-www-form-urlencoded" \
-d "param1=value1&param2=value2" \
http://localhost:8080/form

curl -H "Content-Type:application/json" \
-d '{"key1":"value1", "key2":"value2"}' \
http://localhost:8080/form

在您的代碼中,您接受的是應用程序/ x-www-form-urlencoded,因此請使用第一個

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM