node.js にリクエストを送信する Web アプリケーションがあります。Node モジュールが Spring REST モジュールを呼び出しています。 ノード呼び出しへの私のWebアプリケーションは以下の通りです
$.ajax({
type : "POST",
url : "http://localhost:9090/module",
dataType : 'json',
data : msgDetails,
xhrFields : { withCredentials : true },
success : function(data) {
console.log("getInbox" + JSON.stringify(data.message));
}, error : function(data) {
alert("Error in the AJAX response." + data);
} });
私のノードは以下の通りです
var express = require("express"),
app = express(),
http = require("http").createServer(app);
var requestObj = require('request');
responseBody = "";
indexresponseBody = null;
app.use(express.bodyParser());
app.post('/module', function(req, res){
var tempInbox = "";
var tmp = req;
var authKey = req.body.pubKey;
var reqBody = req.body;
console.log("POST"+" - "+JSON.stringify(reqBody)+" - "+authKey);
restCMCall("http://ip:port/restmodule/controller/call", req, res,authKey,reqBody);
//res.end(JSON.stringify(body));
res.header('Content-Type', 'application/json');
//resp.header('Charset', 'utf-8');
res.send({"name1":"name"});
});
function restCMCall(resturl, req, res, authKey,reqBody){
var i = 0;
console.log("reqBody :- "+reqBody+" resturl "+resturl+" "+i++);
requestObj({
url : resturl,
method : "POST",
headers : { "Content-Type" : "application/json","pubKey":authKey},
body : JSON.stringify(reqBody)
},
function (error, resp, body) {
tempInbox = body;
console.log(resp+" inbox body :- "+" -------- "+i++);
//resp.writeHead(200, {'Content-Type':'application/json','charset':'UTF-8'});
//res.write(JSON.stringify({"hello":"xxx"}));
//res.end(JSON.stringify(body));
res.header('Content-Type', 'application/json');
//resp.header('Charset', 'utf-8');
res.send(body);
}
);
console.log(i++);
}
これまで、Spring モジュールから応答を取得でき、ノード コンソールに出力できました。しかし、Web アプリケーションからのリクエストに応じてこのレスポンスを追加しようとすると、送信されません。
Firefox ブラウザーの firebug では、次のように表示されます。
Response Headers
Connection keep-alive
Content-Length 21
Content-Type application/json
Date Mon, 07 Oct 2013 16:13:38 GMT
X-Powered-By Express
ただし、応答タブは空白です。
エクスプレス モジュールを使用して、スプリング レスト Web サービス呼び出し node.js を呼び出しています。
何か不足している場合はお知らせください。私も使ってみました
response.json(body)
しかし、これも機能していません。