An open API service indexing awesome lists of open source software.

https://github.com/olistic/hapi-overriding

Override HTTP verbs
https://github.com/olistic/hapi-overriding

Last synced: about 2 months ago
JSON representation

Override HTTP verbs

Awesome Lists containing this project

README

          

# hapi-overriding

**hapi-overriding** is a [hapi](https://github.com/hapijs/hapi) plugin that allows you to override the method of an incoming request, using HTTP verbs such as PUT and DELETE in places where the client doesn't support it. This package was inspired in [hapi-method-override](https://github.com/ubaltaci/hapi-method-override).

[![Build Status](https://travis-ci.org/olistic/hapi-overriding.svg?branch=v0.1.4)](https://travis-ci.org/olistic/hapi-overriding)
[![Coverage Status](https://coveralls.io/repos/moliveraf/hapi-overriding/badge.svg?branch=master)](https://coveralls.io/r/moliveraf/hapi-overriding?branch=master)

## Install

```sh
$ npm install hapi-overriding
```

## Usage

**hapi-overriding** works by extending the `onRequest` step of the request lifecycle. The extension function checks, for any incoming POST request, if the path ends with one of the defined action names (e.g. `destroy`). If so, it will change the method to the corresponding HTTP verb (e.g. `DELETE`). It will also change the url of the request, removing the action part and keeping your routes clean.

## Example

Client-side:
```html

Delete user

```

Server-side:
```javascript
var Hapi = require('hapi');
var server = new Hapi.Server();

server.connection({ port: 3000 });

server.register(require('hapi-overriding'), function(err) {

if (err) {
throw err;
}

server.route({
method: 'DELETE',
path: '/users/{id}', // No `/destroy` at the end!
handler: function (request, reply) { ... }
});

server.start();
});
```

## Options

The action names may be customized by passing the following options when registering the plugin:

- `put` - the action that represents the PUT verb. Defaults to `update`.
- `delete` - the action that represents the DELETE verb. Defaults to `destroy`.