https://github.com/ladjs/koa-isajax
Middleware for koajs 2 to check if the incoming request is an Ajax request.
https://github.com/ladjs/koa-isajax
Last synced: 8 months ago
JSON representation
Middleware for koajs 2 to check if the incoming request is an Ajax request.
- Host: GitHub
- URL: https://github.com/ladjs/koa-isajax
- Owner: ladjs
- Fork: true (bjufre/koa-isajax)
- Created: 2021-12-03T12:56:58.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2021-12-03T13:07:01.000Z (over 4 years ago)
- Last Synced: 2025-01-11T10:41:20.066Z (over 1 year ago)
- Language: JavaScript
- Size: 10.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# 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`.](http://expressjs.com/en/api.html#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
```javascript
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);
```