@ladjs/koa-isajax

2.0.0 • Public • Published

koa-isajax

Express req.xhr equivalent for Koa 2 applications. Middleware for Koa 2 that sets a boolean on the ctx.state on whether or not the request is and Ajax request.

This middleware is the equivalent to Express req.xhr.

Installation

$ npm install koa-isajax --save

Options

isajax(setVary) accepts an argument setVary which is a String that defaults to 'X-Requested-With'. If you pass false, then ctx.vary(setVary) will not be called immediately.

This sets the Vary header so that AJAX and HTML responses on the same page do not conflict:

Example

import Koa from 'koa';
import isajax from 'koa-isajax';

const app = new Koa();

app.use(isajax());
app.use(async (ctx) => {
    if (ctx.state.xhr) {
        // Ajax request.
    } else  {
        // Not ajax request.
    }
});

app.listen(3000);

Package Sidebar

Install

npm i @ladjs/koa-isajax

Weekly Downloads

12

Version

2.0.0

License

ISC

Unpacked Size

2.38 kB

Total Files

3

Last publish

Collaborators

  • titanism
  • shadowgate15
  • niftylettuce
  • shaunwarman
  • spence-s