-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
36 lines (30 loc) · 1.05 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
const mongodb = require('mongodb');
const express = require('express');
const app = express();
let db;
const connectionOptions = { poolSize: process.env.MONGO_POOLSIZE || 1 };
mongodb.MongoClient.connect('mongodb://localhost:27017/test', connectionOptions, function(err, database) {
if (err) throw err;
db = database.db('test');
// create some documents required for demonstration
db.collection('test').insertOne({"a": "b"});
app.listen(process.env.PORT || 3000, function() {
console.log(`Express.js server up.`);
});
});
/*
For demonstration purposes, this route will always return slowly due to an intentionally slow query (5seconds per document)
*/
app.get('/slow', function (req, res) {
db.collection('test').find({'$where': 'sleep(5000) || true'}).toArray(function(err, cursor) {
return res.json({"docCount": 'docs'});
});
});
/*
This route should always return quickly
*/
app.get('/fast', function (req, res) {
db.collection('test').countDocuments({}, function(err, count) {
return res.json({'documentCount': count});
});
});