( 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 are the values that have been stored in the request.session. If nothing is set it is replaced by "not set" by the controller.
First name:
Last name:
<%= person.first_name %>
<%= person.last_name %>
You can navigate back to the form to try some other variables.

/examples/processing_url_and_post_parameters
You may 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

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());