( generated on the "<%= source %>" )

Processing URL and POST parameters

This example will demonstrate how to process URL parameters and POST parameters as well as some redirect and rendering logic.

The form below here will submit the data to the post action after which there will be a redirect to a different parameterized route which in turn will save the values to the session and finally redirect to the output screen.
First name:
Last name:


<form action="/examples/processing_url_and_post_parameters" method="post">
  <input type="text" value="" name="person[first_name]">
  <input type="text" value="" name="person[last_name]">
  <button type="submit">send</button>
</form>

Then below here we have the Client Express configuration:

var express = require('express');

exports.processingUrlAndPostParameters = function() {
  var server = express.createServer();

  server.get('/', function(request, response) {
    response.render('processing_url_and_post_parameters/form', 
                    {title: 'client.express.js - client', source: 'client' });
  });

  server.post('/', function(request, response) {
    var person = request.body.person;
    response.redirect('/person/' + person.first_name + '/' + person.last_name);
  });

  server.get('/person/:first_name/:last_name', function(request, response) {
    request.session.person = {
      first_name: request.params.first_name,
      last_name: request.params.last_name
    };
    response.redirect('/output');
  });

  server.get('/output', function(request, response) {
    var person = request.session.person || {
      first_name: 'not set',
      last_name: 'not set'
    };
    response.render('processing_url_and_post_parameters/output', 
                    {title: 'client.express.js - client', source: 'client', person: person });
  });

  return server;
};

Which will result in the following routes being loaded:

  Route loaded: GET  /examples/processing_url_and_post_parameters
  Route loaded: GET  /examples/processing_url_and_post_parameters/person/:first_name/:last_name
  Route loaded: GET  /examples/processing_url_and_post_parameters/output
  Route loaded: POST /examples/processing_url_and_post_parameters

The reason why the route is different from how this Client Express server is configured is because it is mounted in a different Client Express server with a different base path.

server.use('/examples/processing_url_and_post_parameters', 
           ClientExpress.processingUrlAndPostParameters());
You may have noticed but you can also shortcut the process by triggering the second defined GET request directly, for example:

/examples/processing_url_and_post_parameters/person/Mark/Nijhof
/examples/processing_url_and_post_parameters/person/Mona/Nijhof
And finally you can jump directly to the output page to inspect the request.session values.

/examples/processing_url_and_post_parameters/output