This repository has been archived by the owner on Nov 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
91 lines (73 loc) · 2.81 KB
/
app.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
const express = require('express');
const app = express();
const mongodb = require('mongodb');
const mongoClient = mongodb.MongoClient;
const guid = require('guid');
const bodyParser = require('body-parser');
const fs = require('fs');
// ensure express parses body (and this has to be done before the routes below)
app.use(bodyParser.json());
// secret key stuff (so that we can configure a mongodb connection)
function getMongoConnectionString() {
if (process.env.mongoconnection)
return process.env.mongoconnection.trim();
return fs.readFileSync('mongoconnection', 'utf8').trim();
}
// mongo db stuff
var db;
mongoClient.connect(getMongoConnectionString(), (err, database) => {
if (err) return console.log(err);
db = database;
app.listen(3000, () => {
console.log('listening on port 3000')
});
});
// ********
// ROUTES!!
// ********
// nice hello world API call for testing/demo purposes
app.all('/hello', (req, res) => {
res.send('Hello world!');
});
// if any request is made to root, list avilable routes
app.all('/', (req, res) => {
var cursor = db.listCollections().toArray((err, results) => {
if (err) return res.status(500).json(err);
return res.json(results.map((c) => c.name));
});
});
// http post (create)
app.post('/*', (req, res) => {
if (!req.body) return res.status(400).json("You must provide a body in a post");
if (!Object.keys(req.body).length) return res.status(400).json("You must provide a JSON object with something in it");
db.collection(req.path).save(req.body, (err, result) => {
if (err) return res.status(500).json(err);
return res.json(result);
})
});
// http get (read)
app.get('/*', (req, res) => {
var cursor = db.collection(req.path).find().toArray((err, results) => {
if (err) return res.status(500).json(err);
return res.json(results);
});
});
// http put (update/replace)
app.put('/*', (req, res) => {
if (!req.body) return res.status(400).json("You must provide a body in a post");
if (!Object.keys(req.body).length) return res.status(400).json("You must provide a JSON object with something in it");
db.collection(req.path).update(req.body, (err, result) => {
if (err) return res.status(500).json(err);
return res.json(result);
})
});
// http delete
app.delete('/*', (req, res) => {
if (!req.body) return res.status(400).json("You must provide a body in a post");
if (!Object.keys(req.body).length) return res.status(400).json("You must provide a JSON object");
if (!req.body._id) return res.status(400).json('You must provide an \'_id\' property to delete');
db.collection(req.path).deleteOne({_id: new mongodb.ObjectID(req.body._id)}, (err, result) => {
if (err) return res.status(500).json(err);
return res.json(result);
});
})