1. 程式人生 > >【node.js】GET/POST請求、Web 模塊

【node.js】GET/POST請求、Web 模塊

ima esp ges 實例 bst node.js 參數 模塊 pos

獲取GET請求內容

node.js 中 url 模塊中的 parse 函數提供了這個功能。

var http = require(‘http‘);
var url = require(‘url‘);
var util = require(‘util‘);
 
http.createServer(function(req, res){
    res.writeHead(200, {‘Content-Type‘: ‘text/plain; charset=utf-8‘});
    res.end(util.inspect(url.parse(req.url, true)));
}).listen(
3000);

獲取 URL 的參數

var http = require(‘http‘);
var url = require(‘url‘);
var util = require(‘util‘);
 
http.createServer(function(req, res){
    res.writeHead(200, {‘Content-Type‘: ‘text/plain‘});
 
    // 解析 url 參數
    var params = url.parse(req.url, true).query;
    res.write("網站名:" + params.name);
    res.write(
"\n"); res.write("網站 URL:" + params.url); res.end(); }).listen(3000);

獲取 POST 請求內容

var http = require(‘http‘);
var querystring = require(‘querystring‘);
 
var postHTML = 
  ‘<html><head><meta charset="utf-8"><title>菜鳥教程 Node.js 實例</title></head>‘ +
  ‘<body>‘ +
  ‘<form method="post">‘ +
  ‘網站名: <input name="name"><br>‘ +
  ‘網站 URL: <input name="url"><br>‘ +
  ‘<input type="submit">‘ +
  ‘</form>‘ +
  ‘</body></html>‘;
 
http.createServer(
function (req, res) { var body = ""; req.on(‘data‘, function (chunk) { body += chunk; }); req.on(‘end‘, function () { // 解析參數 body = querystring.parse(body); // 設置響應頭部信息及編碼 res.writeHead(200, {‘Content-Type‘: ‘text/html; charset=utf8‘}); if(body.name && body.url) { // 輸出提交的數據 res.write("網站名:" + body.name); res.write("<br>"); res.write("網站 URL:" + body.url); } else { // 輸出表單 res.write(postHTML); } res.end(); }); }).listen(3000);

使用 Node 創建 Web 服務器(服務端創建服務器,解析請求,讀取文件內容,發送響應數據(響應頭部、響應內容))

創建server.js

var http = require(‘http‘);
var fs = require(‘fs‘);
var url = require(‘url‘);


// 創建服務器
http.createServer( function (request, response) {  
   // 解析請求,包括文件名
   var pathname = url.parse(request.url).pathname;
   
   // 輸出請求的文件名
   console.log("Request for " + pathname + " received.");
   
   // 從文件系統中讀取請求的文件內容
   fs.readFile(pathname.substr(1), function (err, data) {
      if (err) {
         console.log(err);
         // HTTP 狀態碼: 404 : NOT FOUND
         // Content Type: text/plain
         response.writeHead(404, {‘Content-Type‘: ‘text/html‘});
      }else{             
         // HTTP 狀態碼: 200 : OK
         // Content Type: text/plain
         response.writeHead(200, {‘Content-Type‘: ‘text/html‘});    
         
         // 響應文件內容
         response.write(data.toString());        
      }
      //  發送響應數據
      response.end();
   });   
}).listen(8081);

// 控制臺會輸出以下信息
console.log(‘Server running at http://127.0.0.1:8081/‘);

創建一個 index.html

<html>
<head>
<title>Sample Page</title>
</head>
<body>
Hello World!
</body>
</html>

進入http://127.0.0.1:8081/index.html後

技術分享

使用 Node 創建 Web 客戶端(向服務端請求,請求的選項,不斷更新數據,數據接收完成)

var http = require(‘http‘);

// 用於請求的選項
var options = {
   host: ‘localhost‘,
   port: ‘8081‘,
   path: ‘/index.htm‘  
};

// 處理響應的回調函數
var callback = function(response){
   // 不斷更新數據
   var body = ‘‘;
   response.on(‘data‘, function(data) {
      body += data;
   });
   
   response.on(‘end‘, function() {
      // 數據接收完成
      console.log(body);
   });
}
// 向服務端發送請求
var req = http.request(options, callback);
req.end();

運行後得到結果

技術分享

【node.js】GET/POST請求、Web 模塊