Permalink
Browse files

Initial commit

  • Loading branch information...
tj committed Apr 16, 2014
0 parents commit 2d22eeee009eff704f2ba2da36be0c50991f3530
Showing with 269 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +3 −0 .travis.yml
  3. +16 −0 History.md
  4. +11 −0 Makefile
  5. +31 −0 Readme.md
  6. +31 −0 example.js
  7. +39 −0 index.js
  8. +31 −0 package.json
  9. +106 −0 test/test.js
@@ -0,0 +1 @@
node_modules
@@ -0,0 +1,3 @@
language: node_js
node_js:
- "0.11"
@@ -0,0 +1,16 @@
1.1.0 / 2014-03-05
==================
* add 'next' param to path params list as the last param
* add route.all() handler
1.0.2 / 2013-11-27
==================
* fix fallthrough bug
1.0.1 / 2013-11-15
==================
* fix: check if .method matches
@@ -0,0 +1,11 @@
test:
@NODE_ENV=test ./node_modules/.bin/mocha \
--require should \
--harmony-generators \
--reporter spec \
--bail
clean:
@rm -rf node_modules
.PHONY: test clean
@@ -0,0 +1,31 @@
# koa-statsd
Statsd middleware.
## Installation
```js
$ npm install koa-statsd
```
## Options
- `prefix` optional statsd prefix ('.' is appended)
- `host` optional statsd host
- `port` optional statsd port
## Metrics
- `request.count` request counter
- `request.<method>.count` request counter
- `request.<method>.<path>.count` request counter
- `request.size` request content-length
- `request.duration` request duration
- `request.<method>.duration` request duration
- `request.<method>.<path>.duration` request duration
- `request.addresses` request addresses set
## License
MIT
@@ -0,0 +1,31 @@
var stats = require('./');
var r = require('koa-route');
var koa = require('koa');
var app = koa();
var db = {
tobi: { name: 'tobi', species: 'ferret' },
loki: { name: 'loki', species: 'ferret' },
jane: { name: 'jane', species: 'ferret' }
};
var pets = {
list: function *(){
var names = Object.keys(db);
this.body = 'pets: ' + names.join(', ');
},
show: function *(name){
var pet = db[name];
if (!pet) return this.error('cannot find that pet', 404);
this.body = pet.name + ' is a ' + pet.species;
}
};
app.use(stats());
app.use(r.get('/pets', pets.list));
app.use(r.get('/pets/:name', pets.show));
app.listen(3000);
console.log('listening on port 3000');
@@ -0,0 +1,39 @@
/**
* Module dependencies.
*/
var Stats = require('statsy');
/**
* Initialize stats middleware with `opts`
* which are passed to statsy.
*
* @param {Object} [opts]
* @return {Function}
* @api public
*/
module.exports = function(opts){
opts = opts || {};
var s = new Stats(opts);
return function *stats(next){
// counters
s.incr('request.count');
s.incr('request.' + this.method + '.count');
s.incr('request.' + this.method + '.' + this.path + '.count');
// size
s.histogram('request.size', this.request.length);
// remote addr
s.set('request.addresses', this.ip);
// duration
this.res.on('finish', s.timer('request.duration'));
this.res.on('finish', s.timer('request.' + this.method + '.' + this.path + '.duration'));
yield next;
}
};
@@ -0,0 +1,31 @@
{
"name": "koa-statsd",
"description": "Koa statsd middleware",
"repository": "koajs/statsd",
"version": "0.0.1",
"keywords": [
"koa",
"middleware",
"stats",
"statsd",
"statistics",
"metrics"
],
"files": [
"index.js"
],
"devDependencies": {
"mocha": "*",
"supertest": "*",
"should": "*",
"koa": "koajs/koa",
"koa-route": "~1.1.1"
},
"license": "MIT",
"dependencies": {
"statsy": "^0.1.0"
},
"scripts": {
"test": "make test"
}
}
@@ -0,0 +1,106 @@
var request = require('supertest');
var koa = require('koa');
var methods = require('methods').map(function(method){
// normalize method names for tests
if (method == 'delete') method = 'del';
if (method == 'connect') return; // WTF
return method;
}).filter(Boolean)
var route = require('../');
describe('Koa Route', function(){
methods.forEach(function(method){
var app = koa();
app.use(route[method]('/:user(tj)', function*(user){
this.body = user;
}))
describe('route.' + method + '()', function(){
describe('when method and path match', function(){
it('should 200', function(done){
request(app.listen())
[method]('/tj')
.expect(200)
.expect(method === 'head' ? '' : 'tj', done);
})
})
describe('when only method matches', function(){
it('should 404', function(done){
request(app.listen())
[method]('/tjayyyy')
.expect(404, done);
})
})
describe('when only path matches', function(){
it('should 404', function(done){
request(app.listen())
[method === 'get' ? 'post' : 'get']('/tj')
.expect(404, done);
})
})
})
})
describe('route.all()', function(){
describe('should work with', function(){
methods.forEach(function(method){
var app = koa();
app.use(route.all('/:user(tj)', function*(user){
this.body = user;
}))
it(method, function(done){
request(app.listen())
[method]('/tj')
.expect(200)
.expect(method === 'head' ? '' : 'tj', done);
})
})
})
describe('when patch does not match', function(){
it('should 404', function (done){
var app = koa();
app.use(route.all('/:user(tj)', function*(user){
this.body = user;
}))
request(app.listen())
.get('/tjayyyyyy')
.expect(404, done);
})
})
})
describe('route params', function(){
describe('should include "next" param with', function(){
methods.forEach(function(method){
var app = koa();
app.use(route[method]('/:user(tj)', function*(user, next){
yield next;
}))
app.use(route[method]('/:user(tj)', function*(user, next){
this.body = user;
yield next;
}))
app.use(route[method]('/:user(tj)', function*(user, next){
this.status = 201;
}))
it(method, function(done){
request(app.listen())
[method]('/tj')
.expect(201)
.expect(method === 'head' ? '' : 'tj', done);
})
})
})
})
})

0 comments on commit 2d22eee

Please sign in to comment.