【问题标题】:Java web application node integration issueJava Web 应用程序节点集成问题
【发布时间】:2013-10-14 07:56:22
【问题描述】:

我有一个向我的 node.js 发送请求的 Web 应用程序。节点模块正在调用我的 Spring REST 模块。 我的网络应用程序到节点调用如下

$.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 Browser 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

但响应选项卡为空白。

我正在使用 express 模块调用 spring rest web 服务调用 node.js。

如果我遗漏了什么,请告诉我。我也尝试过使用

response.json(body)

但这也行不通。

【问题讨论】:

  • Web 应用程序和 REST 应用程序都部署在同一台机器上的 tomcat 服务器上,而我的节点应用程序正在另一台机器/服务器上运行。

标签: spring node.js jquery rest express


【解决方案1】:

我相信您应该向 url : "http://localhost:9090/getInbox" 发出请求,因为您尚未在 Node 应用程序中创建与 POST: /module 匹配的端点

【讨论】:

  • 是的,我正在节点模块中向localhost:9090/module 发出请求。请查找更新后的帖子。
猜你喜欢
  • 2020-02-25
  • 1970-01-01
  • 1970-01-01
  • 2011-04-20
  • 1970-01-01
  • 1970-01-01
  • 2012-06-14
  • 2017-04-10
  • 2023-03-23
相关资源
最近更新 更多