<commit_before># Warepot

## Usage

Warepot kind of rapes Express. It does especially routes
in a very non-standard ways.

The main concept behind warepot is the concept of the responder middleware.
Basically no other middleware should be sending the response, they should simply
call `next()` until the responder is reached. The responder then either
send the contents of `res.locals` as JSON, or renders a template.

Essentially routes should just be arrays of `[ path, method, ...middleware ]`.
If the array only consists of functions (middleware), then `express.use` will
be used instead of `express[method]`.

An example route file could look like so:

```
'use strict'

const mw = require('./middleware')

module.exports = [
  [ '/api/articles', 'get', mw.getAll ],
  [ '/api/articles', 'post', mw.create ],
  [ '/api/articles/:id', 'get', mw.findOne ],
  [ '/api/articles/:id', 'put', mw.replace ],
  [ '/api/articles/:id', 'patch', mw.update ],
  [ '/api/articles/:id', 'delete', mw.remove ]
]
```

A simple example `server/server.js`:

```
'use strict'

const bodyParser = require('body-parser')
const express = require('express')
const initRoutes = require('warepot/util/init-routes')

const server = express()

server.set('port', 1337)

const prewares = [
  bodyParser.json(),
]

const postwares = [
  require('warepot/ensure-found'),
  // transform and log error
  require('warepot/error-handler'),
  // respond
  require('warepot/responder'),
  // handle error in responder
  require('warepot/responder-error'),
]

const routes = [
  ...prewares,
  ...require('./routes/index')
  ...postwares
]

initRoutes(server, routes)

// Start Express server.
server.listen(server.get('port'), () => {
  console.info('[' + chalk.cyan('INIT') + '] HTTP server listening on port %d in %s mode', server.get('port'), server.get('env'))
})

module.exports = server
```

## Configuration

The error handler needs some configuration.

There is a `/sample-config` directory that contains example configuration
files.  These should be copied into your project's `/server/config` directory
and edited to suit your needs.

## Debugging

Warepot (and most other server-side modules from TCB) uses the excellent
[Debug](https://github.com/visionmedia/debug) plugin.

To debug everything, simply set `DEBUG=warepot` as an environment variable. To
debug specific parts, set (for example) `DEBUG=warepot:responder`. Debuggable
parts are currently:

- warepot:errorhandler
- warepot:responder

## Caveats

### Static routes also matching dynamic routes

If you have 

```
module.exports = [
  [ '/api/users/me', mw.getCurrent ],
  [ '/api/users/:id', isAdmin, mw.findById ]
]
```<commit_msg>Made all code fields js<commit_after># Warepot

## Usage

Warepot kind of rapes Express. It does especially routes
in a very non-standard ways.

The main concept behind warepot is the concept of the responder middleware.
Basically no other middleware should be sending the response, they should simply
call `next()` until the responder is reached. The responder then either
send the contents of `res.locals` as JSON, or renders a template.

Essentially routes should just be arrays of `[ path, method, ...middleware ]`.
If the array only consists of functions (middleware), then `express.use` will
be used instead of `express[method]`.

An example route file could look like so:

```js
'use strict'

const mw = require('./middleware')

module.exports = [
  [ '/api/articles', 'get', mw.getAll ],
  [ '/api/articles', 'post', mw.create ],
  [ '/api/articles/:id', 'get', mw.findOne ],
  [ '/api/articles/:id', 'put', mw.replace ],
  [ '/api/articles/:id', 'patch', mw.update ],
  [ '/api/articles/:id', 'delete', mw.remove ]
]
```

A simple example `server/server.js`:

```js
'use strict'

const bodyParser = require('body-parser')
const express = require('express')
const initRoutes = require('warepot/util/init-routes')

const server = express()

server.set('port', 1337)

const prewares = [
  bodyParser.json(),
]

const postwares = [
  require('warepot/ensure-found'),
  // transform and log error
  require('warepot/error-handler'),
  // respond
  require('warepot/responder'),
  // handle error in responder
  require('warepot/responder-error'),
]

const routes = [
  ...prewares,
  ...require('./routes/index')
  ...postwares
]

initRoutes(server, routes)

// Start Express server.
server.listen(server.get('port'), () => {
  console.info('[' + chalk.cyan('INIT') + '] HTTP server listening on port %d in %s mode', server.get('port'), server.get('env'))
})

module.exports = server
```

## Configuration

The error handler needs some configuration.

There is a `/sample-config` directory that contains example configuration
files.  These should be copied into your project's `/server/config` directory
and edited to suit your needs.

## Debugging

Warepot (and most other server-side modules from TCB) uses the excellent
[Debug](https://github.com/visionmedia/debug) plugin.

To debug everything, simply set `DEBUG=warepot` as an environment variable. To
debug specific parts, set (for example) `DEBUG=warepot:responder`. Debuggable
parts are currently:

- warepot:errorhandler
- warepot:responder

## Caveats

### Static routes also matching dynamic routes

If you have 

```js
module.exports = [
  [ '/api/users/me', mw.getCurrent ],
  [ '/api/users/:id', isAdmin, mw.findById ]
]
```
{'fraction_non_alphanumeric': 0.14232488822652756, 'fraction_numerical': 0.0014903129657228018, 'mean_word_length': 4.20756547041707, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1476734', 'n_tokens_mistral': 1685, 'n_tokens_neox': 1645, 'n_words': 549}