Our under-performing server is a very simple HTTP application that calculates the average of all the data points we have inserted.
Let's take the following code and save it as server.js:
const MongoClient = require('mongodb').MongoClientconst express = require('express')const app = express()var url = 'mongodb://localhost:27017/test';MongoClient.connect(url, function(err, db) { if (err) { throw err } const collection = db.collection('data') app.get('/hello', (req, res) => { collection.find({}).toArray(function sum (err, data) { if (err) { res.send(err) return } const total = data.reduce((acc, d) => acc + d.value, 0) const result = total / data.length res.send('' + result) }) }) app.listen(3000)})
Now we'll run it:
$ node server.js ...