<commit_before>
### Installation

    $ npm install express

### Creating A Server

 To create an instance of the _express.HTTPServer_, simply invoke the _createServer()_ method. With our instance _app_ we can then define routes based on the HTTP verbs, in this example _app.get()_.

    var app = require('express').createServer();
    
    app.get('/', function(req, res){
      res.send('hello world');
    });
    
    app.listen(3000);

### Creating An HTTPS Server

 To initialize a _express.HTTPSServer_ we do the same as above, however we
 pass an options object, accepting _key_, _cert_ and the others mentioned in node's [https documentation](http://nodejs.org/docs/v0.3.7/api/https.html#https.createServer).
 
     var app = require('express').createServer({ key: ... });

### Configuration

Express supports arbitrary environments, such as _production_ and _development_. Developers
can use the _configure()_ method to setup needs required by the current environment. When
_configure()_ is called without an environment name it will be run in _every_ environment 
prior to the environment specific callback.

In the example below we only _dumpExceptions_, and respond with exception stack traces
in _development_ mode, however for both environments we utilize _methodOverride_ and _bodyParser_.
Note the use of _app.router_, which can (optionally) be used to mount the application routes,
otherwise the first call to _app.get()_, _app.post()_, etc will mount the routes.

    app.configure(function(){
  		app.use(express.methodOverride());
  		app.use(express.bodyParser());
  		app.use(app.router);
  	});
	
  	app.configure('development', function(){
  		app.use(express.static(__dirname + '/public'));
  		app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
  	});
	
    app.configure('production', function(){
      var oneYear = 31557600000;
      app.use(express.static(__dirname + '/public', { maxAge: oneYear }));
      app.use(express.errorHandler());
    });

For internal and arbitrary settings Express provides the _set(key[, val])_, _enable(key)_, _disable(key)_ methods:

     app.configure(function(){
        app.set('views', __dirname + '/views');
        app.set('views');
        // => "/absolute/path/to/views"
        
        app.enable('some feature');
        // same as app.set('some feature', true);
        
        app.disable('some feature');
        // same as app.set('some feature', false);
     
        app.enabled('some feature')
        // => false
     });

To alter the environment we can set the _NODE_ENV_ environment variable, for example:

    $ NODE_ENV=production node app.js

This is _very_ important, as many caching mechanisms are _only enabled_ when in production.

### Settings

Express supports the following settings out of the box:

  * _home_ Application base path used for _res.redirect()_ and transparently handling mounted apps.
  * _views_ Root views directory defaulting to **CWD/views**
  * _view engine_ Default view engine name for views rendered without extensions
  * _view options_ An object specifying global view options

### Routing

Express utilizes the HTTP verbs to provide a meaningful, expressive routing API.
For example we may want to render a user's account for the path _/user/12_, this
can be done by defining the route below. The values associated to the named placeholders 
are available as `req.params`.

    app.get('/user/:id', function(req, res){
  		res.send('user ' + req.params.id);
  	});

A route is simple a string which is compiled to a _RegExp_ internally. For example
when _/user/:id_ is compiled, a simplified version of the regexp may look similar to:

    \/user\/([^\/]+)\/?

Regular expression literals may also be passed for complex uses. Since capture
groups with literal _RegExp_'s are anonymous we can access them directly `req.params`. So our first capture group would be _req.params[0]_ and the second would follow as _req.params[1]_.

    app.get(/^\/users?(?:\/(\d+)(?:\.\.(\d+))?)?/, function(req, res){
        res.send(req.params);
    });

Curl requests against the previously defined route:

       $ curl http://dev:3000/user
       [null,null]
       $ curl http://dev:3000/users
       [null,null]
       $ curl http://dev:3000/users/1
       ["1",null]
       $ curl http://dev:3000/users/1..15
       ["1","15"]

Below are some route examples, and the associated paths that they
may consume:

     "/user/:id"
     /user/12

     "/users/:id?"
     /users/5
     /users

     "/files/*"
     /files/jquery.js
     /files/javascripts/jquery.js

     "/file/*.*"
	 /files/jquery.js
	 /files/javascripts/jquery.js
	
	 "/user/:id/:operation?"
	 /user/1
	 /user/1/edit
	
	 "/products.:format"
	 /products.json
	 /products.xml

	 "/products.:format?"
	 /products.json
	 /products.xml
	 /products
	 
	 "/user/:id.:format?"
	 /user/12
	 /user/12.json

For example we can __POST__ some json, and echo the json back using the _bodyParser_ middleware which will parse json request bodies (as well as others), and place the result in _req.body_:

    var express = require('express')
      , app = express.createServer();

    app.use(express.bodyParser());

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

    app.listen(3000);

Typically we may use a "dumb" placeholder such as "/user/:id" which has no restrictions, however say for example we are limiting a user id to digits, we may use _'/user/:id(\\d+)'_ which will _not_ match unless the placeholder value contains only digits.

### Passing Route Control

We may pass control to the next _matching_ route, by calling the _third_ argument,
the _next()_ function. When a match cannot be made, control is passed back to Connect,
and middleware continue to be invoked in the order that they are added via _use()_. The same is true for several routes which have the same path defined, they will simply be executed in order until one does _not_ call _next()_ and decides to respond.

	app.get('/users/:id?', function(req, res, next){
		var id = req.params.id;
		if (id) {
			// do something
		} else {
			next();
		}
	});
	<commit_msg>markdown escaping<commit_after>
### Installation

    $ npm install express

### Creating A Server

 To create an instance of the _express.HTTPServer_, simply invoke the _createServer()_ method. With our instance _app_ we can then define routes based on the HTTP verbs, in this example _app.get()_.

    var app = require('express').createServer();
    
    app.get('/', function(req, res){
      res.send('hello world');
    });
    
    app.listen(3000);

### Creating An HTTPS Server

 To initialize a _express.HTTPSServer_ we do the same as above, however we
 pass an options object, accepting _key_, _cert_ and the others mentioned in node's [https documentation](http://nodejs.org/docs/v0.3.7/api/https.html#https.createServer).
 
     var app = require('express').createServer({ key: ... });

### Configuration

Express supports arbitrary environments, such as _production_ and _development_. Developers
can use the _configure()_ method to setup needs required by the current environment. When
_configure()_ is called without an environment name it will be run in _every_ environment 
prior to the environment specific callback.

In the example below we only _dumpExceptions_, and respond with exception stack traces
in _development_ mode, however for both environments we utilize _methodOverride_ and _bodyParser_.
Note the use of _app.router_, which can (optionally) be used to mount the application routes,
otherwise the first call to _app.get()_, _app.post()_, etc will mount the routes.

    app.configure(function(){
  		app.use(express.methodOverride());
  		app.use(express.bodyParser());
  		app.use(app.router);
  	});
	
  	app.configure('development', function(){
  		app.use(express.static(__dirname + '/public'));
  		app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
  	});
	
    app.configure('production', function(){
      var oneYear = 31557600000;
      app.use(express.static(__dirname + '/public', { maxAge: oneYear }));
      app.use(express.errorHandler());
    });

For internal and arbitrary settings Express provides the _set(key[, val])_, _enable(key)_, _disable(key)_ methods:

     app.configure(function(){
        app.set('views', __dirname + '/views');
        app.set('views');
        // => "/absolute/path/to/views"
        
        app.enable('some feature');
        // same as app.set('some feature', true);
        
        app.disable('some feature');
        // same as app.set('some feature', false);
     
        app.enabled('some feature')
        // => false
     });

To alter the environment we can set the _NODE_ENV_ environment variable, for example:

    $ NODE_ENV=production node app.js

This is _very_ important, as many caching mechanisms are _only enabled_ when in production.

### Settings

Express supports the following settings out of the box:

  * _home_ Application base path used for _res.redirect()_ and transparently handling mounted apps.
  * _views_ Root views directory defaulting to **CWD/views**
  * _view engine_ Default view engine name for views rendered without extensions
  * _view options_ An object specifying global view options

### Routing

Express utilizes the HTTP verbs to provide a meaningful, expressive routing API.
For example we may want to render a user's account for the path _/user/12_, this
can be done by defining the route below. The values associated to the named placeholders 
are available as `req.params`.

    app.get('/user/:id', function(req, res){
  		res.send('user ' + req.params.id);
  	});

A route is simple a string which is compiled to a _RegExp_ internally. For example
when _/user/:id_ is compiled, a simplified version of the regexp may look similar to:

    \/user\/([^\/]+)\/?

Regular expression literals may also be passed for complex uses. Since capture
groups with literal _RegExp_'s are anonymous we can access them directly `req.params`. So our first capture group would be _req.params[0]_ and the second would follow as _req.params[1]_.

    app.get(/^\/users?(?:\/(\d+)(?:\.\.(\d+))?)?/, function(req, res){
        res.send(req.params);
    });

Curl requests against the previously defined route:

       $ curl http://dev:3000/user
       [null,null]
       $ curl http://dev:3000/users
       [null,null]
       $ curl http://dev:3000/users/1
       ["1",null]
       $ curl http://dev:3000/users/1..15
       ["1","15"]

Below are some route examples, and the associated paths that they
may consume:

     "/user/:id"
     /user/12

     "/users/:id?"
     /users/5
     /users

     "/files/*"
     /files/jquery.js
     /files/javascripts/jquery.js

     "/file/*.*"
	 /files/jquery.js
	 /files/javascripts/jquery.js
	
	 "/user/:id/:operation?"
	 /user/1
	 /user/1/edit
	
	 "/products.:format"
	 /products.json
	 /products.xml

	 "/products.:format?"
	 /products.json
	 /products.xml
	 /products
	 
	 "/user/:id.:format?"
	 /user/12
	 /user/12.json

For example we can __POST__ some json, and echo the json back using the _bodyParser_ middleware which will parse json request bodies (as well as others), and place the result in _req.body_:

    var express = require('express')
      , app = express.createServer();

    app.use(express.bodyParser());

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

    app.listen(3000);

Typically we may use a "dumb" placeholder such as "/user/:id" which has no restrictions, however say for example we are limiting a user id to digits, we may use _'/user/:id(\\\\d+)'_ which will _not_ match unless the placeholder value contains only digits.

### Passing Route Control

We may pass control to the next _matching_ route, by calling the _third_ argument,
the _next()_ function. When a match cannot be made, control is passed back to Connect,
and middleware continue to be invoked in the order that they are added via _use()_. The same is true for several routes which have the same path defined, they will simply be executed in order until one does _not_ call _next()_ and decides to respond.

	app.get('/users/:id?', function(req, res, next){
		var id = req.params.id;
		if (id) {
			// do something
		} else {
			next();
		}
	});
	
{'fraction_non_alphanumeric': 0.11329648894668401, 'fraction_numerical': 0.00959037711313394, 'mean_word_length': 3.2785118219749654, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4065508', 'n_tokens_mistral': 3659, 'n_tokens_neox': 3440, 'n_words': 1345}