Skip to content
This repository was archived by the owner on May 1, 2025. It is now read-only.

Commit 106051d

Browse files
committed
Merge pull request #8 from Turistforeningen/docs/add-examples
Add example Express app using MongoQS
2 parents 992591b + 9befb32 commit 106051d

File tree

7 files changed

+207
-4
lines changed

7 files changed

+207
-4
lines changed

docker-compose.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
mongo:
2+
image: mongo:latest
3+
14
dev:
25
image: node:0.10-slim
6+
links:
7+
- mongo
38
working_dir: /usr/src/app
49
volumes:
510
- ".:/usr/src/app"

examples/app.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict';
2+
3+
var express = require('express');
4+
var app = module.exports = express();
5+
6+
var MongoQS = require('../index');
7+
8+
// Create a new Mongo QueryString parser
9+
var qs = new MongoQS({
10+
custom: {
11+
bbox: 'geojson',
12+
near: 'geojson'
13+
}
14+
});
15+
16+
app.get('/api/places', function(req, res) {
17+
res.set('Content-Type', 'application/json');
18+
19+
// Parse the request query parameters
20+
var query = qs.parse(req.query);
21+
var collection = require('./db').db.collection('places');
22+
var cursor = collection.find(query).limit(3);
23+
24+
cursor.stream().pipe(require('JSONStream').stringify()).pipe(res);
25+
});
26+
27+
if (!module.parent) {
28+
require('./db').once('ready', function() {
29+
app.listen(3000);
30+
console.log('Express started on port 3000');
31+
});
32+
}

examples/data.json

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
[{
2+
"name": "Solrenningen",
3+
"geojson": {
4+
"type": "Point",
5+
"coordinates": [6.13037, 61.00607]
6+
},
7+
"tags": ["Ved", "Mat", "Båt"]
8+
},{
9+
"name": "Norddalshytten",
10+
"geojson": {
11+
"type": "Point",
12+
"coordinates": [5.99579, 61.01340]
13+
},
14+
"tags": ["Ved", "Mat", "Tåke"]
15+
},{
16+
"name": "Åsedalen",
17+
"geojson": {
18+
"type": "Point",
19+
"coordinates": [6.22032, 60.96244]
20+
},
21+
"tags": ["Ved", "Mat", "Stekeovn"]
22+
},{
23+
"name": "Vatnane",
24+
"geojson": {
25+
"type": "Point",
26+
"coordinates": [6.32607, 61.02105]
27+
},
28+
"tags": ["Ved"]
29+
},{
30+
"name": "Selhamar",
31+
"geojson": {
32+
"type": "Point",
33+
"coordinates": [6.26495, 60.91275]
34+
},
35+
"tags": ["Ved", "Mat", "Stekeovn", "Båt"]
36+
},{
37+
"name": "Vardadalsbu",
38+
"geojson": {
39+
"type": "Point",
40+
"coordinates": [5.89279, 60.94477]
41+
},
42+
"tags": ["Ved", ""]
43+
}]

examples/db.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict';
2+
3+
var EventEmitter = require('events').EventEmitter;
4+
var MongoClient = require('mongodb').MongoClient;
5+
var inherits = require('util').inherits;
6+
7+
var Mongo = function(uri) {
8+
EventEmitter.call(this);
9+
10+
this.db = null;
11+
12+
var $this = this;
13+
14+
new MongoClient.connect(uri, function(err, db) {
15+
if (err) { throw err; }
16+
17+
$this.db = db;
18+
$this.emit('ready');
19+
});
20+
21+
return this;
22+
};
23+
24+
inherits(Mongo, EventEmitter);
25+
26+
module.exports = new Mongo('mongodb://mongo:27017/test');

examples/test.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
'use strict';
2+
3+
var assert = require('assert');
4+
var request = require('supertest');
5+
6+
var data = require('./data');
7+
var app = request(require('./app'));
8+
var db = require('./db');
9+
10+
describe('Example App', function() {
11+
before(function(done) {
12+
if (db.db) { return done(); }
13+
db.once('ready', done);
14+
});
15+
16+
before(function(done) {
17+
db.db.dropDatabase(done);
18+
});
19+
20+
before(function(done) {
21+
db.db.collection('places').createIndex({geojson: '2dsphere'}, done);
22+
});
23+
24+
before(function(done) {
25+
db.db.collection('places').insertMany(data, done);
26+
});
27+
28+
var url = '/api/places';
29+
30+
it('returns all them places', function(done) {
31+
app.get(url)
32+
.expect(200)
33+
.expect(function(res) {
34+
assert.equal(res.body.length, 3);
35+
})
36+
.end(done);
37+
});
38+
39+
it('returns places matching name', function(done) {
40+
app.get(url + '?name=Vatnane')
41+
.expect(200)
42+
.expect(function(res) {
43+
assert.equal(res.body.length, 1);
44+
assert.equal(res.body[0].name, 'Vatnane');
45+
})
46+
.end(done);
47+
});
48+
49+
it('returns places near point', function(done) {
50+
app.get(url + '?near=6.13037,61.00607')
51+
.expect(200)
52+
.expect(function(res) {
53+
assert.equal(res.body.length, 3);
54+
assert.equal(res.body[0].name, 'Solrenningen');
55+
assert.equal(res.body[1].name, 'Åsedalen');
56+
assert.equal(res.body[2].name, 'Norddalshytten');
57+
})
58+
.end(done);
59+
});
60+
61+
it('returns places inside bbox', function(done) {
62+
var bbox = [
63+
'5.5419158935546875',
64+
'60.92859723298985',
65+
'6.0363006591796875',
66+
'61.018719220334525'
67+
].join(',');
68+
69+
app.get(url + '?bbox=' + bbox)
70+
.expect(200)
71+
.expect(function(res) {
72+
assert.equal(res.body.length, 2);
73+
assert.equal(res.body[0].name, 'Norddalshytten');
74+
assert.equal(res.body[1].name, 'Vardadalsbu');
75+
})
76+
.end(done);
77+
});
78+
79+
it('returns palces with any of the following tags', function(done) {
80+
app.get(url + '?tags[]=Båt&tags[]=Stekeovn')
81+
.expect(200)
82+
.expect(function(res) {
83+
assert.equal(res.body.length, 3);
84+
assert.equal(res.body[0].name, 'Solrenningen');
85+
assert.equal(res.body[1].name, 'Åsedalen');
86+
assert.equal(res.body[2].name, 'Selhamar');
87+
})
88+
.end(done);
89+
});
90+
});

package.json

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
"test": "test"
1414
},
1515
"scripts": {
16-
"lint": "jshint index.js test.js",
17-
"watch": "mocha -w -b -c --check-leaks -R progress test.js",
18-
"test": "mocha -b -c --check-leaks -R spec test.js",
16+
"lint": "jshint *.js examples",
17+
"watch": "mocha -w -b -c --check-leaks -R progress test.js examples/test.js",
18+
"test": "mocha -b -c --check-leaks -R spec test.js examples/test.js",
1919
"semantic-release": "semantic-release"
2020
},
2121
"keywords": [
@@ -41,9 +41,13 @@
4141
},
4242
"homepage": "https://github.com/Turistforeningen/node-mongo-querystring",
4343
"devDependencies": {
44+
"JSONStream": "^1.0.7",
45+
"express": "^4.13.3",
4446
"jshint": "^2.8.0",
4547
"mocha": "^2.3.4",
48+
"mongodb": "^2.0.52",
4649
"qs": "^5.2.0",
47-
"semantic-release": "^4.3.5"
50+
"semantic-release": "^4.3.5",
51+
"supertest": "^1.1.0"
4852
}
4953
}

wercker.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
box: node:0.10
22

3+
services:
4+
- mongo:latest
5+
36
build:
47
steps:
58
- script:

0 commit comments

Comments
 (0)