Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

What is preferred way to submit form with express?

What is preferred way to submit form with express?


In my first Node project Express/Express-resource libraries are used and Jade for templating.

According to docs default mappings are generated. Among them we can find:

PUT     /forums/:forum       ->  update

Yet, I do not see easy way to Submit values.

How to submit creation/update?

Jade form can be created easily and body parser as well, but how to submit this form? Please note that express-resource defines PUT method (not POST).

Problem courtesy of: nrph


From the Express guide:

When using methods such as PUT with a form, we can utilize a hidden input named _method, which can be used to alter the HTTP method. To do so we first need the methodOverride middleware, which should be placed below bodyParser so that it can utilize it’s req.body containing the form values.



And in your form:

Update: As I understand the new comments from the asker, nrph wants a way to submit a form with the PUT method, using ajax. Here is a solution using jQuery:

// Use this submit handler for all forms in document
$(document).on('submit', 'form', function(e) {
  // Form being submitted
  var form = e.currentTarget;
  // Issue an ajax request
    url: form.action,          // the forms 'action' attribute
    type: 'PUT',               // use 'PUT' (not supported in all browsers)
                               // Alt. the 'method' attribute (form.method)
    data: $(form).serialize(), // Serialize the form's fields and values
    success: function() {},
    error: function() {}
  // Prevent the browser from submitting the form
Solution courtesy of: Linus Gustav Larsson Thiel


View additional discussion.

This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

What is preferred way to submit form with express?


Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription