Nodejsは翌日勉強します。

4762 ワード

今日はNodejs公式サイトからapi(一般的にはこれをモジュールと呼ぶ)の練習を開始しました。java JDKに似ています。一日目のコードによると、
 //   http  
 var http = require('http');
 //    httpServer    8081  
 http.createServer(function (req, res) {
//       ,      --> Hello, World.
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.write('Hello, World.');
  res.end(); //      
 }).listen(8081);

以上は簡単なウェブプログラムであり、サーバからアプリケーションまでの全ての流れを提供しています。ここでの業務ロジックは非常に簡単です。ページに文字列を出力するだけです。
次に、学習後の理解に基づいて、マニュアルで下記のコードを作成しました。異なる要求からのわずかな処理は、Hello, World.と同様である。
まず、先にプログラムの入り口を定義します。jsはjava # DispatcherServlet関数がありませんが、mainを新たに作って、まずコードをコード化する計画はありません。次に、index.jsを新たに作成し、初日に学習した部分の内容を保存するために、ファイル名のように、server.jsと同様の機能であり、コードは以下の通りである。
 //  http   url     java  Map\\\\List    
var http = require('http'),
    url = require('url') ;
 function start() {
  //      ,            ,           
  function onRequest(req,res) {
   //    url       parse      ,      node    document  
   var pathName = url.parse(req.url).pathname ;
   console.log('request path ',pathName) ;
   //         
   res.writeHead(200,{'Content-Type':'text/plain'}) ;

   res.write('Hello,World.') ;
   res.end() ;
  }

  http.createServer(onRequest).listen(8081) ;
  console.log('server has started.') ;
 }

 //   start         
 //export
 exports.start = start ;
次にserverに戻り、次のコードを作成する。
//         server  
 var server = require('./server') ;

//      server  
 server.start() ;
次のコマンドを実行できます。
node index.js
アクセスindex.jsは、出力を見ることができます。
上記のコードは業務ロジックをhttp://localhost:8081に置いています。実際の仕事では明らかに不合理です。業務ロジックの変更は女性よりも顔色を変えるのが早いので、serverにおいて結果が分かります。ここで調整が必要です。私たちは最初にserver方式で方法をサーバ端に伝達し、いつまでも書ききれないjava #servletを通じて異なる機能の配信を行います。ここでもしルーティングテーブルのようなものを提供すれば、同様の目的を達成できます。getオブジェクトはif....else if.... else if....の形で出現するので、この機能は、例えば、
var handle = {} ;
handle['/'] = root ; //        
handle['/start'] = start ; //        
handle['/upload'] = upload ; //        
このように、異なる機能はルーティングテーブルと同様に関連されています。例えば、
//dispatcher.js
 function start() {
  console.log('call /start') ;

  return 'call /start' ;
 }

 function upload() {
  console.log('call /upload') ;
  return 'call /upload' ;
 }

 function root() {
  console.log('call /') ;
  return 'call /' ;
 }

 exports.start = start ;
 exports.upload = upload ;
 exports.root = root ;
以上のコードは異なる需要の機能関数を表します。次に、ルーティング転送機能のモジュールを提供します。例えば、
//router.js
 function route(path,handle) {
  console.log('route path ',path) ;

  var fun = handle[path] ;
  if(typeof fun === "function") {
   return fun.call(null,arguments) ;
  }else {
   console.log('unknown path.') ;
   return 'unknown path.' ;
  }
 }

 exports.route = route ;
次に、javaScriptおよびkey-valueのコードを適切に修正する必要があります。
server.js
//server.js
 //  http   url     java  Map\\\\List    
var http = require('http'),
    url = require('url') ;
 //            ,handle        ,route    
function start(handle,route) {
  //      ,            ,           
  function onRequest(req,res) {
   //    url       parse      ,      node    document  
   var pathName = url.parse(req.url).pathname ;
   console.log('request path ',pathName) ;
   //         
   res.writeHead(200,{'Content-Type':'text/plain'}) ;
  //  route         ,            ,            
   //dispatcher
   var content = route(pathName,handle) ;
   console.log('return content',content) ;

   res.write(content) ;
   //res.write('Hello,World.') ;
   res.end() ;
  }

  http.createServer(onRequest).listen(8081) ;
  console.log('server has started.') ;
 }

 //   start         
 //export
 exports.start = start ;

index.js
//index.js

 var server = require('./server'),
     router = require('./router'),
     dispatcher = require('./dispatcher');

 var handle = {} ;
 handle['/'] = dispatcher.root ;
 handle['/start'] = dispatcher.start ;
 handle['/upload'] = dispatcher.upload ;
 server.start(handle,router.route) ;
index.jsを実行し、マッピングパスにアクセスして、異なる出力情報を見ることができます。
これで翌日のnodejsの勉強はここで終わります。おやすみなさい。以上の内容に間違いがあったら、ぜひご指摘ください。ありがとうございます。
server.js node index.js http://localhost:8081/upload 142