-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
73 lines (59 loc) · 1.89 KB
/
server.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
'use strict';
const express = require('express');
const app = express();
const mongoose = require('mongoose');
const Art = require('./models/Art');
const path = require('path');
//GET total number of art
app.get('/api/art/total/', (req, res) => {
Art.find({}, function (err, artList) {
if (err) return res.status(400).json(err);
res.json({ total: artList.length });
});
});
// GET an art object by ID
app.get('/api/art/:artId', (req, res) => {
Art.findById(req.params.artId, (err, artObj) => {
if (err) return res.status(400).json(err);
res.json(artObj);
});
});
// GET nearby art
app.get('/api/art/near/:location', (req, res) => {
let coords = req.params.location.split(',');
coords = coords.map((coordinate) => {
return Number(coordinate);
});
Art.aggregate([
{
$geoNear: {
near: { type: 'Point', coordinates: coords },
distanceField: 'distance',
distanceMultiplier: 0.000621371,
maxDistance: 16093.4,
spherical: true,
limit: 1000
}
}
], (err, results) => {
if (err) return res.status(400).json(err);
res.json(results);
});
});
// POST to add like
app.post('/api/art/:artId/like', (req, res) => {
Art.findOne({ _id: req.params.artId }, (err, artObj) => {
if (err) return res.status(400).json(err);
artObj.likes += 1;
artObj.save((err, result) => {
if (err) return res.status(400).json(err);
res.status(200).json(result);
});
});
});
app.use(express.static(__dirname + '/cohpa/www/'));
app.get('/*', (req, res) => {
res.sendFile(path.resolve(__dirname + '/cohpa/www/index.html'));
});
mongoose.connect('mongodb://localhost:27017/cohpa');
app.listen(process.env.PORT, process.env.IP);