2017-01-11 22 views
3

この質問は以前何度も聞いたことがありますが、まだ私はこれを理解するのに苦労しています。私はjsファイルのセットを持っています。最初のものはのparamsを抽出index.jsノードは送信後にヘッダーを設定できません

app.all('/backend/*/*', function(req, res){ // backend/product/getProduct 
    serviceType = req.params[0]; 
    methodType = req.params[1]; 

    exports.serviceType= serviceType; 
    exports.methodType= methodType; 

    main.checkService() 
}); 

ここでイムあるとmain.jsファイルに

main.js

function checkService(){ 

    switch(index.serviceType){ 
     case 'product': 
      product.checkMethod(); 
      break; 

     default : 
      console.log('no such service') 
    } 
} 

checkServiceメソッドを呼び出し、それはproduct.jsファイルに

function checkMethod(){ 

    var methodName = index.methodType, 
     res = index.res, 
     req = index.req; 

    switch(methodName){ 
     case 'samplePost': 
      var body = req.body; 
      proHan.samplePost(body,function(data,msg,status){ 
       sendRes(data,msg,status); 
      }); 
      break; 

     default : 
      console.log('no such method') 
    } 

    function sendRes(jsonObj,msg,status){ 
     var resObj = { 
      status : status, 
      result : jsonObj, 
      message : msg 
     } 
     res.json(resObj); 
    } 
を移動します

は最初、それは、http REQが実行finisedたら、コールバックが結果を返すとsendResメソッドを呼び出すと、私は、共通のファイルを書かれたHTTP REQを送信するために、JSON

function samplePost(jsonString,cb){ 
    var res = config.setClient('nodeSample'); 
    // jsonString = JSON.parse(jsonString); 
    res.byKeyField('name').sendPost(jsonString,function(data,msg,status){ 
     cb(data,msg,status); 
    }); 
} 

を送るhandler.jssamplePost方法に移動します。それはconfig.js

function setClient(_cls){ 
    var client = new Client(url); 
    return client; 
} 

function parentClient(url){ 
    this.postBody = { 
     "Object":{}, 
     "Parameters":{ 
     "KeyProperty":"" 
     } 
    }; 


} 

function paramChild(){ 
    parentClient.apply(this, arguments); 

    this.byKeyField = function(_key){ 
     this.postBody.Parameters.KeyProperty = _key; 
     return this; 
    } 
} 

function Client(url){ 
    parentClient.apply(this, arguments); 

    this.sendPost = function(_body,cb){ 
     _body = (_body) || {}; 

     this.postBody.Object = _body; 

     var options = { 
      host : 'www.sample.com', 
      port : 3300, 
      path: '/somrthing', 
      headers: { 
       'securityToken' : '123' 
      } 
     }; 

     options.method = "POST"; 

     var req = http.request(options, function(response){ 
     var str = '' 
     response.on('data', function (chunk) { 
      str += chunk; 
     }); 

     response.on('end', function() { 
      cb(JSON.parse('[]'),'success',200) 
     }); 

     }); 
     //This is the data we are posting, it needs to be a string or a buffer 
     req.on('error', function(response) { 
     cb(JSON.parse('[]'),response.errno,response.code) 
     }); 

     req.write(JSON.stringify(this.postBody)); 
     req.end(); 
    } 
} 


paramChild.prototype = new parentClient(); 
Client.prototype = new paramChild(); 

私は最初のreqその作業を送るが、サーバがクラッシュした後、再びからです。私はres.endメソッドをコールバックメソッドで再び呼び出すことができないようです。どのように私はこれを修正することができます。ありがとうございました。

+0

gitのリンクはありますか?試してみてください –

+0

@Shankar Shastri残念ながらno –

+0

どのようにhttpが動作するので、1つのリクエストに複数の応答を送ることはできません... 2つの応答をマージして、同時に2つの応答を送信するか、http ... 、 –

答えて

2

res.endを2回呼び出すことはできません。ここでは、基本的なノードサーバーでコールバックを処理する簡単な例を示します。

const http = require('http'); 

const hostname = '127.0.0.1'; 
const port = 4242; 
let something = true; 

function callback(req, res) { 
    something = !something; 
    res.setHeader('Content-Type', 'text/plain'); 
    res.end('Callback Hello World\n'); 
} 

const server = http.createServer((req, res) => { 
    res.statusCode = 200; 
    if (something) { 
    callback(req, res); 
    } else { 
    something = !something; 
    res.setHeader('Content-Type', 'text/plain'); 
    res.end('Hello World\n'); 
    } 
}); 

server.listen(port, hostname,() => { 
    console.log(`Server running at http://${hostname}:${port}/`); 
}); 
関連する問題