2015-01-21 3 views
-2

내 node.js 서버에서 json 응답을 받고 웹 페이지에 응답을 표시하는 방법을 알고 싶습니다.내 node.js 서버에서 Json 응답을 얻고 Web (ejs) 페이지에 표시하는 방법

다음을 사용할 수 있습니다

app.engine('.html', require('ejs').__express);

:

아래

var request = require("request"), 
    username = req.body.username, 
    password = req.body.password, 
    url = "https://api.ecoachsolutions.com/main.php?ecoachsignin=1&server=remote&user="+username+"&pass="+password; 


    console.log("url is "+url); 
    request.get(
    { 
     url : url 
    }, 
    function (error, response, body) { 
     // Do more stuff with 'body' here 
     if (!error && response.statusCode == 200) { 
     var json_body = JSON.parse(body); 
     console.log(json_body); 
     var msg = json_body.profile.user;//this is the message i want to show on my web page(msg) 
    console.log(msg); // get json response 
    } 
    } 
); 

답변

1

먼저 EJS를 사용하여 명시 등록해야합니다 JSON 코드에서 요청 reponse입니다

:하고 EJS 당신이 작업 예제가 필요한 경우

<%= msg %> 

를 통해 그 액세스 할 수 있습니다 그 후보기

res.render('index.html', {msg: json_body.profile.user}); 

에 데이터를 전달, 좋은에서 찾을 수 있습니다 https://github.com/strongloop/express/tree/master/examples/ejs

관련 문제