无法POST /付款(Node.js和Stripe)

时间:2014-11-06 20:10:03

标签: javascript node.js stripe-payments

我在名为payment.ejs

的文件中嵌入了条纹签出
<form action="" method="POST">
  <script
    src="https://checkout.stripe.com/checkout.js" class="stripe-button"
    data-key="pk_test_ODW7OJfVhlRJEgFY0ppWzwEE"
    data-amount="2000"
    data-name="Demo Site"
    data-description="2 widgets ($20.00)"
    data-image="/128x128.png">
  </script>
</form>

我访问该页面并输入一张被接受的信用卡。但是,然后我得到了错误

  

无法发帖/付款

在我的app.js中我有

 app.post('payment', function(req, res){
    //stripe
        console.log('posted')

    var stripeToken = req.body.stripeToken;
    var charge = stripe.charges.create({
      amount: 1000, // amount in cents, again
      currency: "usd",
      card: stripeToken,
      description: "payinguser@example.com"
    }, function(err, charge) {
      if (err && err.type === 'StripeCardError') {
        console.log("CARD DECLINED");
        res.send('error')
      }
      else {
          console.log("CARD ACCEPTED");
          res.send('ok')

      }
    });
 });

根据说明。我在这里看不到什么错误。有任何想法吗?

1 个答案:

答案 0 :(得分:2)

路由应包含前面的/

app.post('/payment', function (req, res) {
    //    ^

    // ...
});

ExpressJS&#39;路由部分基于匹配pathrequested url,其中包括来自&#34; root&#34;的每个/。 (在主机名和端口之后)。另一部分是method

app.use(function (req, res, next) {
    console.log(req.method); // "POST"
    console.log(req.path);   // "/payment"
                             //  ^
    next();
});

注意:是否需要跟踪/或缺少匹配取决于strict routingRouter&#39; s strict option是否已启用。

  

启用严格路由,默认情况下&#34; / foo&#34;和&#34; / foo /&#34;路由器对待它们。