<commit_before>---
layout: post
title: RESTful forms in express
category: posts
---

Express by default is quite agnostic about application structure. Recently, I plugged 
in [express-resource](https://github.com/visionmedia/express-resource) for *resourceful routing*.

Basically in a RESTful architecture you support a handful of operations supporting CRUD (Create, read, update, delete).
HTTP has request methods such as GET, POST, PUT, DELETE.

Using these request methods we can implement REST actions:

Example: A **forum** resource
-------
* GET     /forums              ->  index
* GET     /forums/new          ->  new
* POST    /forums              ->  create
* GET     /forums/:forum       ->  show
* GET     /forums/:forum/edit  ->  edit
* PUT     /forums/:forum       ->  update
* DELETE  /forums/:forum       ->  destroy

To implement this scheme we can simply require `express-resource` in our project:

```
var Resource = require('express-resource')
```

This enables `app.resource` which maps our url scheme to the above actions

```
app.resource('forums', require('./controllers/forum'));
```

Define controllers/forum.js

```
exports.index = function(req, res){
  res.send('forum index');
};

exports.new = function(req, res){
  res.send('new forum');
};

exports.create = function(req, res){
  res.send('create forum');
};

exports.show = function(req, res){
  res.send('show forum ' + req.forum.title);
};

exports.edit = function(req, res){
  res.send('edit forum ' + req.forum.title);
};

exports.update = function(req, res){
  res.send('update forum ' + req.forum.title);
};

exports.destroy = function(req, res){
  res.send('destroy forum ' + req.forum.title);
};
```

So.. going to `http://localhost:3000/forums` is mapped to `forum.index` 

This makes routing **much easier** and consistent since you basically replace

```
app.get('/forums', Forum.index)
app.get('/forums/new', Forum.new)
app.get('/forums/:id', Forum.show)
app.post('/forums', Forum.create)
app.put('/forums/:id', Forum.update)
// etc..
```

with 
```
app.resource('forums', require('./controllers/forum'));<commit_msg>Update 2013-04-15-restfulforms.md<commit_after>---
layout: post
title: RESTful forms in express
category: posts
---

Express by default is quite agnostic about application structure. Recently, I plugged 
in [express-resource](https://github.com/visionmedia/express-resource) for *resourceful routing*.

Basically in a RESTful architecture you support a handful of operations supporting CRUD (Create, read, update, delete).
HTTP has request methods such as GET, POST, PUT, DELETE.

Using these request methods we can implement REST actions:

Example: A **forum** resource
-------
* GET     /forums              ->  index
* GET     /forums/new          ->  new
* POST    /forums              ->  create
* GET     /forums/:forum       ->  show
* GET     /forums/:forum/edit  ->  edit
* PUT     /forums/:forum       ->  update
* DELETE  /forums/:forum       ->  destroy

To implement this scheme we can simply require `express-resource` in our project:

```
var Resource = require('express-resource')
```

This enables `app.resource` which maps our url scheme to the above actions

```
app.resource('forums', require('./controllers/forum'));
```

Define controllers/forum.js

<code>
exports.index = function(req, res){
  res.send('forum index');
};

exports.new = function(req, res){
  res.send('new forum');
};

exports.create = function(req, res){
  res.send('create forum');
};

exports.show = function(req, res){
  res.send('show forum ' + req.forum.title);
};

exports.edit = function(req, res){
  res.send('edit forum ' + req.forum.title);
};

exports.update = function(req, res){
  res.send('update forum ' + req.forum.title);
};

exports.destroy = function(req, res){
  res.send('destroy forum ' + req.forum.title);
};
</code>

So.. going to `http://localhost:3000/forums` is mapped to `forum.index` 

This makes routing **much easier** and consistent since you basically replace

```
app.get('/forums', Forum.index)
app.get('/forums/new', Forum.new)
app.get('/forums/:id', Forum.show)
app.post('/forums', Forum.create)
app.put('/forums/:id', Forum.update)
// etc..
```

with 
```
app.resource('forums', require('./controllers/forum'));
{'fraction_non_alphanumeric': 0.15824124147660476, 'fraction_numerical': 0.0037620503174229957, 'mean_word_length': 3.511134676564157, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, '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': '951271', 'n_tokens_mistral': 1372, 'n_tokens_neox': 1301, 'n_words': 384}