-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
40 lines (32 loc) · 978 Bytes
/
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
37
38
39
40
var express = require('express');
var _ = require('underscore');
var app = express();
var server = require('http').createServer(app);
var config = require('./lib/config');
var path = require('path');
var async;
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/speedtest');
var speedSchema = mongoose.Schema({
date: Date,
ping: Number,
down: Number,
up: Number
});
var Speed = mongoose.model('speed', speedSchema);
var json = function(res, data) {
res.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8' });
if(typeof data === "string") res.write(data);
else res.write(JSON.stringify(data));
res.end();
};
app.use(express.static(__dirname + '/static'));
app.get('/speeds', function(req, res) {
var values;
Speed.find(function (err, speeds) {
if (err) return console.error(err);
console.log(speeds);
json(res, speeds);
})
});
server.listen(process.env.PORT || config.port);