Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Express.js get http method in controller

I am building a registration form (passport-local as authentication, forms as form helper).

Because the registration only knows GET and POST I would like to do the whole handling in one function.

With other words I am searching after something like:

exports.register = function(req, res){     if (req.isPost) {        // do form handling     }     res.render('user/registration.html.swig', { form: form.toHTML() }); }; 
like image 418
dev.pus Avatar asked Jun 23 '12 08:06

dev.pus


People also ask

What is get method in Express js?

get() function routes the HTTP GET Requests to the path which is being specified with the specified callback functions. Basically it is intended for binding the middleware to your application. Parameters: path: It is the path for which the middleware function is being called.

What does Express router () do?

The express. Router() function is used to create a new router object. This function is used when you want to create a new router object in your program to handle requests.

What is Express JSON ()?

json() is a built-in middleware function in Express. This method is used to parse the incoming requests with JSON payloads and is based upon the bodyparser. This method returns the middleware that only parses JSON and only looks at the requests where the content-type header matches the type option.


1 Answers

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

like image 176
dev.pus Avatar answered Oct 01 '22 12:10

dev.pus