1

Je rencontre un problème avec lequel le même code (qui accède à AWS Api Gateway) s'exécute très bien s'il est envoyé de cette façon: node app.js, mais produit 307 Rediriger, lorsque soumis à partir de l'application Node/Express.comment accéder correctement à AWS Api Gateway à partir de l'application Web du noeud

est ici le "stand-alone" programme de nœud de fichier unique (de app.js):

var http = require("https"); 

var options = { 
     "method": "POST", 
     "hostname": "ipzjnsvxnd.execute-api.us-west-2.amazonaws.com", 
      "port": null, 
      "path": "/DEV/execution", 
       "headers": { 
         "cache-control": "no-cache", 
          "postman-token": "b929e970-fe22-0f4f-e659-117890fda955" 
           } 
}; 

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); 
         console.log(body.toString()); 
         }); 
}); 

req.write("{\n \"input\": \"{ \\\"account_key\\\": \\\"9990\\\", \\\"acc1\\\": \\\"1235813\\\", \\\"acc2\\\": \\\"13711\\\",\\\"amount\\\": \\\"1000.00\\\", \\\"city\\\": \\\"BrandonTown\\\" }\",\n \"name\": \"RequiredUniqueValueGoesHere19090\",\n \"stateMachineArn\": \"arn:aws:states:us-west-2:217465658899:stateMachine:FICO_StateMachine3\"\n}"); 
req.end(); 

Et est ici le même code, comme une partie d'une application Web Node/Express:

module.exports = function(app) { 

    var querystring = require('querystring'); 
    var http = require('http'); 
    http.post = require('http-post'); 

    app.get('*', function(req, res) { 
     res.sendfile('./public/index.html'); 
    }); 

    app.post("/customerinfo", function(req, res) { 

     var options = { 
      "method": "POST", 
      "hostname": "ipzjnsvxnd.execute-api.us-west-2.amazonaws.com", 
      "path": "/DEV/execution", 
      "headers": { 
       "cache-control": "no-cache", 
       "postman-token": "b929e970-fe22-0f4f-e659-117890fda955" 
      } 
     }; 

      var req1 = http.request(options, function (res1) { 
      var chunks = []; 

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

      res1.on("end", function() { 
       var body = Buffer.concat(chunks); 
       console.log(body.toString()); 
      }); 
     }); 

     req1.write("{\n \"input\": \"{ \\\"account_key\\\": \\\"9990\\\", \\\"acc1\\\": \\\"1235813\\\", \\\"acc2\\\": \\\"13711\\\",\\\"amount\\\": \\\"1000.00\\\", \\\"city\\\": \\\"BrandonTown\\\" }\",\n \"name\": \"RequiredUniqueValueGoesHere1239091\",\n \"stateMachineArn\": \"arn:aws:states:us-west-2:217465658899:stateMachine:FICO_StateMachine3\"\n}"); 
     req1.end(); 
    }); 

}; 

Le premier fonctionne bien lorsqu'il est soumis: noeud app.js La seconde: on revient

<html> 
<head><title>307 Temporary Redirect</title></head> 
<body bgcolor="white"> 
<center><h1>307 Temporary Redirect</h1></center> 
<hr><center>CloudFront</center> 
</body> 
</html> 

<html> 
<head><title>307 Temporary Redirect</title></head> 
<body bgcolor="white"> 
<center><h1>307 Temporary Redirect</h1></center> 
<hr><center>CloudFront</center> 
</body> 
</html> 

<html> 
<head><title>307 Temporary Redirect</title></head> 
+1

Vous devez modifier le code 'http = require (« http »)' à 'http = require ('https')' dans votre application express. –

+0

cela a bien fonctionné - merci. Si vous publiez ceci comme solution, je serai heureux d'accepter. –

+0

Heureux que cela a fonctionné. Ajouté ma réponse! –

Répondre

0

Vous nee d pour changer le code http = require('http') à http = require('https') à l'intérieur de votre application express.

Aussi, jetez un oeil à request, il est un client HTTP avec le soutien de HTTPS et a également beaucoup d'autres fonctionnalités intégrées.