节点js:无法在heroku上发布POST错误

时间:2017-12-29 03:55:05

标签: node.js heroku

我正在编写一个简单的服务器来处理与节点js的支付交易。我开始使用heroku项目模板并添加了“checkout”路由逻辑。当我在localhost上部署它时 - 它对我来说很好。但是在我将它部署到heroku服务器之后,我收到了“无法POST / checkout”错误。我的index.js代码如下:

const express = require('express'), bodyParser = require('body-parser');
const path = require('path')
const PORT = process.env.PORT || 5000
var router = express.Router();
var checkout = require('./routes/checkout');
var braintree = require('braintree');
const app = express()
 .use(express.static(path.join(__dirname, 'public')))
 .set('views', path.join(__dirname, 'views'))
 .set('view engine', 'ejs')
 .get('/', (req, res) => res.render('pages/index'))

.use(bodyParser.urlencoded({extended: true}))
.post('/checkout', (req, res) => {
  console.log(req.body.paymentMethodNonce);
  //res.send(req.body);

  var gateway = braintree.connect({
                                  environment: braintree.Environment.Sandbox,
                                  merchantId: "merchantId",
                                  publicKey: "publicKey",
                                  privateKey: "privateKey"
                                  });


  // Use the payment method nonce here
  //console.log(req.body);
  var nonceFromTheClient = req.body.paymentMethodNonce;
  var amount = req.body.amount;
   //Create a new transaction for $10
              var newTransaction = gateway.transaction.sale({
                                                            //amount:'10.00',
                                                            amount: amount,
                                                            //paymentMethodNonce: "fake-valid-nonce",
                                                            paymentMethodNonce: nonceFromTheClient,
                                                            options: {
                                                            // This option requests the funds from the transaction
                                                            // once it has been authorized successfully
                                                            submitForSettlement: true
                                                            }
                                                            }, function(error, result) {
                                                            if (result) {
                                                            res.send(result);
                                                            } else {
                                                            res.status(500).send(error);
                                                            }
                                                            });


  })
  .listen(PORT, () => console.log(`Listening on ${ PORT }`))

4 个答案:

答案 0 :(得分:0)

@Elena您可以在post方法之后使用静态文件

答案 1 :(得分:0)

重新部署并运行以下命令后,

问题得以解决:

heroku login
heroku git:clone -a repository_name

用我的代码替换源代码(没有更改,但不包括node_modules文件夹)

git add .
git commit -m "added updated code"
git push heroku master

答案 2 :(得分:0)

如果您使用的是免费版的Heroku,则只能发送不接收的数据。

答案 3 :(得分:-1)

const express = require('express');
const bodyParser = require('body-parser');
var checkout = require('./routes/checkout');
var braintree = require('braintree');
const path = require('path')
const PORT = process.env.PORT || 5000
var router = express.Router();
const app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));


app.use(express.static(path.join(__dirname, 'public')));
app.set("views", (__dirname + "/views"));
app.set("view engine", "ejs");

app.get('/', (req, res) => res.render('pages/index'))
app.post('/checkout', (req, res) => {

  console.log(req.body.paymentMethodNonce);

  var gateway = braintree.connect({
        environment: braintree.Environment.Sandbox,
        merchantId: "merchantId",
        publicKey: "publicKey",
        privateKey: "privateKey"
  });

    var nonceFromTheClient = req.body.paymentMethodNonce;
    var amount = req.body.amount;

    //Create a new transaction for $10
    var newTransaction = gateway.transaction.sale({
            //amount:'10.00',
            amount: amount,
            //paymentMethodNonce: "fake-valid-nonce",
            paymentMethodNonce: nonceFromTheClient,
            options: {
            // This option requests the funds from the transaction
            // once it has been authorized successfully
            submitForSettlement: true
            }
        }, function(error, result) {
                if (result) {
                    res.send(result);
                } else {
                    res.status(500).send(error);
                }
           }
    )
});