Express.js get http method in controller Express.js get http method in controller express express

Express.js get http method in controller


The answer was quite easy

exports.register = function(req, res) {    if (req.method == "POST") {       // do form handling    }    res.render('user/registration.html.swig', { form: form.toHTML() });};

But I searched a long time for this approach in the express guide.

Finally the node documentation has such detailed information:http://nodejs.org/api/http.html#http_http_request_options_callback


Now you can use a package in npm => "method-override", which provides a middle-ware layer that overrides the "req.method" property.

Basically your client can send a POST request with a modified "req.method", something like /registration/passportID?_method=PUT.

The

?_method=XXXXX

portion is for the middle-ware to identify that this is an undercover PUT request.

The flow is that the client sends a POST req with data to your server side, and the middle-ware translates the req and run the corresponding "app.put..." route.

I think this is a way of compromise. For more info: method-override