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.

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

  1. Captain says:

    You might have missed method=”post” in the form tag.

  2. hj says:

    captain, how many ways are there of iteration in jade, obtaining a list from the server?

  3. Captain says:

    Jade has its own iterators and you can use regular JavaScript iterators also. There is a whole chapter dedicated to Jade in my book “Express Web Application Development”, got get it – http://www.amazon.com/Express-Application-Development-Hage-Yaapa/dp/1849696543

  4. Amarant says:

    Hi! Do you know any way to iterate over req.param() from a Post request? My idea is this

    app.post(“*”),function(req, res, next){
    for each(param in req.param){
    console.log(param)} }

    I have tried numerous variations of this code including using Object.keys(req.param)

    But my efforts so far has resulted at best in a long list of the word “undefined” in my console…

  5. andrea says:

    I try to use it but when I run appear

    Username: undefined

    and chrome console error 404 (not found) , i did not set any action yet because is just something that I’m triyng to do..

    Any suggestion please, thank you very much

    HTML CODE

    JS CODE

    var express = require(‘express’);
    var app = express();

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

    var server = app.listen(4000, function () {

    var host = server.address().address;
    var port = server.address().port;

    console.log(‘Example app listening at http://%s:%s’, host, port);

    });

  6. Karthick says:

    Hi,

    Can you attach the source for this example using Node.js?

  7. sara says:

    Hi! how to handle a POST request in node js with an array of users?
    I’m new with node js.

    var users=[{name:”Sara”, surname:”S”, ….}]……..
    …..
    ..app.post(‘/session’)….

Make a Comment