繁体   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