I am Hack Sparrow
Captain of the Internets.

POST / GET Request Handling in Node.js Express

How to handle GET and POST request in Node.js Express

URL and forms processing are two of the many factors that make the Web the dynamic and useful platform it is. Any server-side technology that is worth it's salt should implement ways to handle the HTTP GET and POST request efficiently.

Let's find out how to handle GET and POST requests in the Node.js Web framework Express.js with some examples.

Handle GET Request

HTML GET form:

<form method="get" action="/">
<input type="text" name="username">
<input type="submit">
</form>

Handler:

app.get('/', function(req, res) {
res.send('Username: ' + req.query['username']);
});

A good way to remember req.query is that the GET data is stored in the URL query string - query comes from the query string.

Handle POST Request

HTML POST form:

<form method="post" action="/">
<input type="text" name="username">
<input type="submit">
</form>

Handler code:

app.post('/', function(req, res) {
res.send('Username: ' + req.body.username);
};

There it is! Easy GET and POST request handling in Node.js Express framework.

18 Responses to “POST / GET Request Handling in Node.js Express”

  1. Ivan says:

    1. You miss the closing parenthesis at your handlers.
    2. To handle a POST request the bodyParser middleware should be used: app.use(express.bodyParser());

  2. Captain says:

    @Ivan, thanks for pointing out the error.

    Express adds the bodyParser middleware if you generate the app using the express command. If you are creating the app yourself, you definitely need to manually include it manually.

  3. Piotr Perak says:

    First handler should be app.get not app.post.

  4. Captain says:

    @Piotr indeed. Fixed.

  5. Jasdeep Khalsa says:

    Very good, simple tutorial!!! 🙂 Pretty sure this is StackOverflow material.

  6. Thomas says:

    Thank you very much for this simple and accurate article.
    I have been searching for this and would have become desperate if I would not find it.
    THANK YOU

  7. samer says:

    im new in node.js but i wrote some examples.if any one want interst i publish a simple tutorial about how to capture dara form : http://www.stirring-interactive.com/blog/node-js-in-a-nutshell/

  8. Salman Zaman says:

    Hi:

    What if the input type was hidden and value contains an Javascript Array Object. How should I parse the “arrayObject” and access each element of the array in my Node Js Code? Would highly appreciate a prompt response.

  9. Captain says:

    You can’t have a JS array as a value in an HTML form, hidden or not. However, if you are programmatically submitting a form, you can set JS objects as POST data. Literal objects {}, and arrays [] can be accessed using their names in the form if you are using a good HTTP agent library. I recommend superagent – https://github.com/visionmedia/superagent.

  10. Michael says:

    Hello, if I use you second example with “Handle POST Request” on the homne page I get an error message:

    Cannot GET /

    Do you know this problem?

Make a Comment