2017-07-31 11:33:31 +05:30

27 lines
605 B
JavaScript

'use strict'
const MongoClient = require('mongodb').MongoClient
const 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)
})