Skip to content

HeavenDuke/koamethodoverride

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

koamethodoverride

NPM version build status npm download

Full method override for koajs, enabling developer to launch PUT/DELETE/GET/POST request from HTTP form or links.

Install

npm install koamethodoverride

Dependencies

Koa 2(Node >= 0.12.0)
koa-bodyparser

Usage

Configuration your server as follows:

var koa = require('koa');
var bodyParser = require('koa-bodyparser');
var override = require('koamethodoverride');
var server = koa();
server.use(bodyParser());
server.use(override());
server.listen(3000);

Now you can write your form as follows:

<form action="/something" method="POST">
    <input style="display: none;" name="_method" value="delete" />
    <!-- something more -->
</form>

Or link like this:

<a href="/something?_method=delete&something=something" />

Detail

The middle ware will take 3 part of a request into consideration:

  • request.body._method
  • request.query._method(from query string)
  • "x-http-method-override" header(case sensitive)

The priority of above is request.body > request.query > "x-http-method-override" header.

If the override method from a higher priority level exists but the value is not within:

  • GET
  • POST
  • PUT
  • PATCH
  • DELETE

then the middleware will search for lower level override methods, if such don't exist or all go wrong, then the middleware will simply use the original method and will not raise exception.

License

MIT

About

Full method override for koa

Resources

License

Stars

Watchers

Forks

Packages

No packages published