Javascript 通过nodejs在react UI中显示restful服务中的数据
我开发了一个RESTfulWebService,它使用下面的代码显示一个简单的数据Javascript 通过nodejs在react UI中显示restful服务中的数据,javascript,node.js,rest,reactjs,Javascript,Node.js,Rest,Reactjs,我开发了一个RESTfulWebService,它使用下面的代码显示一个简单的数据 package com.mike; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestControll
package com.mike;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MessageController {
@RequestMapping(value="/message",method=RequestMethod.GET)
public Message print(){
return new Message(1,"Hello Mike!!");
}
}
豆子如下所示
package com.mike;
public class Message {
private int id;
private String message;
public Message(int id, String message){
this.id=id;
this.message=message;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getMessage() {
return message;
}
public void setMesssage(String message) {
this.message = message;
}
}
现在我想在Nodejs应用程序中显示这些数据,所以我编写了一个名为app.js的文件,并编写了以下代码来在控制台中显示数据。但我不知道如何将这个nodejs应用程序与reactjsui集成,以便在网页上显示。所以请帮助我,我对node和react都是新手。app.js的代码如下
var http=require('http');
var extServerOptions={
host:'localhost',
port:'8080',
path:'/message',
method:'GET'
};
var mes=[];
var x;
var text="";
function get(){
http.request(extServerOptions,function (res){
res.setEncoding('utf8');
res.on('data',function(data){
mes=JSON.parse(data);
console.log(mes);
for(x in mes){
text+=mes[x]+" ";
};
console.log(text);
});
}).end();
};
get();
请告诉我在react页面中显示数据所需的步骤。哦,天哪,这是个大问题。您希望在一个“catch all”处理程序上交付index.html文件,该处理程序将在该端点获取javascript。不过,在概念上,可能是这样的:编写React组件,向NodeJS发送Ajax请求,NodeJS向Java中的microservice发送请求,NodeJS响应NodeJS,NodeJS反过来响应Ajax调用。我认为这对于一个只返回“嘿,迈克”的微服务来说是“太多了”。或者使用
节点http代理
作为一种服务,它只是将请求代理给微服务。