Ich weiß diese Frage viele Male zuvor gefragt, aber immer noch ich kämpfe, um dies herauszufinden. Ich habe eine Reihe von js-Dateien. erste ist index.js
Knoten kann Header nicht festlegen, nachdem sie gesendet werden
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()
});
in hier im die params Extrahieren und rufen checkService
Methode in main.js
Datei
main.js
function checkService(){
switch(index.serviceType){
case 'product':
product.checkMethod();
break;
default :
console.log('no such service')
}
}
es dann an product.js
Datei verschieben
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);
}
zuerst geht es weiter zu samplePost
Methode in handler.js
sobald die http erf Ausführung finised, Rückruf, um die Ergebnisse zurück und sendRes
Methode aufrufen und die 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);
});
}
senden http erf i eine gemeinsame Datei geschrieben senden. das heißt 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();
als ich das erste erf seine Arbeit schicken, aber von da wieder der Server abstürzt. Es sieht so aus, als ob ich die Methode res.end
nicht erneut in einer Callback-Methode aufrufen kann. Wie kann ich das beheben? Danke.
Haben Sie eine Git-Link? um es auszuprobieren –
@Shankar Shastri leider keine –
Da http funktioniert, können Sie nicht mehrere Antworten auf eine Anfragen senden ... Sie haben zwei Optionen, entweder zwei Antworten zusammenführen, um sofort zu senden oder Socket anstelle von http verwenden, , –