B. Sommer
B. Sommer

Reputation: 61

Render view as result of POST request in Express

I have a basic node app which is integrated with stripe. The client-side javascript generates a token which is sent in a POST request to the server. The server then sends the stripe API the token which makes the payment. I want to redirect the user to a page showing some info e.g. Payment successful or Error making payment.

When I put res.render("/charge", {<some-JSON>}); in the code that handles the POST request, it sends the page /charge as a response to the POST request. Is there a way to send the user a page as a response instead of sending the POST request the response?

Node Snippet:

app.post(req, res) {
    // Some Code
}

Upvotes: 0

Views: 2587

Answers (1)

user6702203
user6702203

Reputation:

From what I gather from your post and comments. You are looking for a way to display Stripe response data into your webpage.

Instead of doing your traditional AJAX request with client-side JavaScript, use a form to submit the data to your server-side.

<form action="/stripe" method="post">
  <input type="text" value="whatever" name="batman">
  <button type="submit">Pay</button>
</form>

On your server-side, create an endpoint, using app.use('/stripe'), as an example, but the endpoint has to match your value in the action="" attribute. Then simply render the page with the stripe response.

app.use('/stripe', function(req, res) {
  // whatever code
  res.render('/charge', {JS OBJECT});
});

Upvotes: 2

Related Questions