2016-06-14 64 views
0

我正在構建一個Web應用程序,用於處理髮布請求,然後對另一個服務器執行POST請求,然後根據返回的信息重定向用戶。使用Express在獨立節點模塊中執行POST請求

最終結果是用戶在他們的用戶名和點擊提交 - >應用過程後,需要用戶名 - >應用程序執行發佈到外部服務器包括用戶名 - >服務器返回服務器的用戶的URL應該開啓 - >應用程序將用戶重定向到該應用程序。

server.js

var express = require('express'); 
var app = express(); 
var bodyParser = require('body-parser'); 
var findUser = require('./findUserInstance') 

// Create application/x-www-form-urlencoded parser 
var urlencodedParser = bodyParser.urlencoded({ extended: false }) 

app.use(express.static('public')); 

app.get('/index.htm', function (req, res) { 
    res.sendFile(__dirname + "/" + "index.htm"); 
}) 

app.post('/process_post', urlencodedParser, function (req, res) { 

    // Prepare output in JSON format 
    response = { 
     username:req.body.username 
    }; 
    var uUrl = findUser.url(response.username); 
    console.log("redirecting to " + uUrl); 
    res.redirect(findUser.url(response.username)); 
    res.end(JSON.stringify(response)); 
}) 

var server = app.listen(8081, function() { 

    var host = server.address().address 
    var port = server.address().port 

    console.log("App listening at http://%s:%s", host, port) 

}) 

findUserInstance.js

exports.url = function(uName) { 

    var http = require("https"); 
    var uUrl; 

    var options = { 
     "method": "POST", 
     "hostname": "removed", 
     "port": null, 
     "path": "removed", 
     "headers": { 
      "appkey": "removed", 
      "content-type": "application/json", 
      "cache-control": "no-cache", 
      "Accept": "application/json", 
      "postman-token": "7d87bcf1-8e11-9717-2f6e-8150a5625acd" 
     } 
    }; 

    var req = http.request(options, function (res) { 
     var chunks = []; 

     res.on("data", function (chunk) { 
      chunks.push(chunk); 
     }); 

     res.on("end", function() { 
      var body = Buffer.concat(chunks); 
      var jsoncontent = JSON.parse(body); 
      uUrl = jsoncontent.rows[0].url; 
      console.log("The following should be: user.instance.url.com) 
      console.log(jsoncontent.rows[0].url); 
      return uUrl; //The information that I want to return to server.js 
     }); 
    }); 

    req.write(JSON.stringify({username: uName})); 
    req.end(); 
} 

問題是與從外部交模塊中的信息返回到server.js模塊,以便它可以執行重定向。目前,我有從函數返回的變量uUrl(正確填充了來自帖子的URL)。但是findUserInstance模塊返回null。

如何從findUserInstance模塊獲取uUrl的值到server.js模塊?

回答

1

@bryan euton良好的反應,你應該返回findUserInstance中的任何對象像諾言! https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Promise

exports.url = function(uName) { 
    return new Promise(function (resolve, reject){ 

    var http = require("https"); 
    var uUrl; 

    var options = { 
     "method": "POST", 
     "hostname": "removed", 
     "port": null, 
     "path": "removed", 
     "headers": { 
     "appkey": "removed", 
     "content-type": "application/json", 
     "cache-control": "no-cache", 
     "Accept": "application/json", 
     "postman-token": "7d87bcf1-8e11-9717-2f6e-8150a5625acd" 
     } 
    }; 

    var req = http.request(options, function (res) { 
     var chunks = []; 

     res.on("data", function (chunk) { 
     chunks.push(chunk); 
     }); 

     res.on("end", function() { 
     var body = Buffer.concat(chunks); 
     var jsoncontent = JSON.parse(body); 
     uUrl = jsoncontent.rows[0].url; 
     console.log("The following should be: user.instance.url.com) 
     console.log(jsoncontent.rows[0].url); 
     resolve(uUrl); //The information resolve promise with your datas 
     }); 

    }); 

    req.write(JSON.stringify({username: uName})); 
    req.end(); 

    }); 

} 
+0

謝謝!這看起來不錯,但我仍然遇到問題。現在url函數返回[object Object](沒有值)。我需要修改server.js中的任何內容嗎? – mcheli

0

是現在uurl在server.js是異步的更改處理:

app.post('/process_post', urlencodedParser, function (req, res) { 

    // Prepare output in JSON format 
    response = { 
    username:req.body.username 
    }; 

    findUser.url(response.username).then(function(uUrl){ 

    console.log("redirecting to " + uUrl); 
    res.redirect(findUser.url(response.username)); 
    res.end(JSON.stringify(response)); 

    }); 

}); 
+0

優秀!完美運作。謝謝soo soo! – mcheli

相關問題