Cannot POST /path

Viewed 821

I've looked through multiple post about this but can't seem to pinpoint the problem. I'm doing a donation page for an organization and need this to check if paypal is even working. It's an error between my form and app.post. Error I get is: Cannot POST /path . Can't use / because its the path for my contact form

   app.get("/donate", (req, res) => res.sendFile(__dirname + "views/donate.html"));
    app.post("/done", (req, res) => {
        const create_payment_json = {
            intent: "sale",
            payer: {
                payment_method: "paypal",
            },
            redirect_urls: {
                return_url: "https://asociacioncorazondiverso.org/donate.html",
                cancel_url: "https://asociacioncorazondiverso.org/donate.html",
            },
            transactions: [
                {
                    item_list: {
                        items: [
                            {
                                name: "Donación",
                                sku: "001",
                                price: "10.00",
                                currency: "USD",
                                quantity: 1,
                            },
                        ],
                    },
                    amount: {
                        currency: "USD",
                        total: "10.00",
                    },
                    description: "Donación",
                },
            ],
        };

        paypal.payment.create(create_payment_json, function (error, payment) {
            if (error) {
                throw error;
            } else {
                for (let i = 0; i < payment.links.length; i++) {
                    if (payment.links[i].rel === "approval_url") {
                        res.redirect(payment.links[i].href);
                    }
                }
            }
        });
    });

Form:

<div class="container-contact100-form-btn">
        <h2>Donación de 10 USD</h2>
        <form action="/done" method="post">
            <button type="submit" class="btn btn-warning"  value="Buy">Donación</button>
        </form>
        </div>
5 Answers

Your code as you posted on GitHub and provided in the comments on the initial question shows a striking difference between what you posted here and what you're actually working with.

Above, your <form>'s action parameter is clearly set to "/done" (which appears to be correct, as that's the path you've defined in your receiving server's code).

However, the code you seem to actually be working with is referencing /path in the same <form> element declaration:

<form action="/path" method="post">
    <button type="submit" class="btn btn-warning"  value="Buy">Donación</button>
</form>

Adjust the action parameter to match what you have in your question above ("/done") so your HTML document POSTs to the correct path/endpoint you've defined.

<form action="/done" method="post">
    <button type="submit" class="btn btn-warning"  value="Buy">Donación</button>
</form>

I saw syntax error in your server.js file on your github link for this code

app.post("/", function(request, response) {

this part is not closed properly, otherwise your code is working if you correctly use paths.

Try to keep the full api url

<form action="http://localhost:3030/done" method="post">
    <button type="submit" class="btn btn-warning"  value="Buy">Donación</button>
</form>

There is a brackets issue in the server.js file that you've hosted on github. The post routes are defined inside the /sendmail route. Move those outside and it should work.

Good evening what if you change you code from

app.post, app.get to 


const router = express.Router();

router.get('/done', (req, res) => { // do smth });

app.use('/', router);

app.use(router);

It should work for you. Just try ;)

Related