-
Notifications
You must be signed in to change notification settings - Fork 0
/
Aurora-mongoDB.js
94 lines (82 loc) · 2.67 KB
/
Aurora-mongoDB.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
92
93
94
'use latest';
import bodyParser from 'body-parser';
import express from 'express';
import Webtask from 'webtask-tools';
import { MongoClient } from 'mongodb';
import { ObjectID } from 'mongodb';
const collection = 'Test';
const server = express();
server.use(bodyParser.json());
//Gets the entire Collection of documents
server.get('/', (req, res, next) => {
const { MONGO_URL } = req.webtaskContext.secrets;
MongoClient.connect(MONGO_URL, (err, db) => {
//const { name } = req.params ;
//res.status(200).send("name");
if (err) return next(err);
db.db('Test').collection(collection).find( {}).toArray( (err2, result) => {
db.close();
if (err2) return next(err2);
res.status(200).send(result);
});
});
});
//Gets documents with a passed JSON file
server.get('/search/', (req, res, next) => {
const { MONGO_URL } = req.webtaskContext.secrets;
MongoClient.connect(MONGO_URL, (err, db) => {
const model = req.body;
//res.status(200).send(name);
if (err) return next(err);
db.db('Test').collection(collection).findOne(model, (err2, result) => {
db.close();
if (err2) return next(err2);
res.status(200).send(result);
});
});
});
//Gets documents with name
server.get('/search/:name', (req, res, next) => {
const { MONGO_URL } = req.webtaskContext.secrets;
MongoClient.connect(MONGO_URL, (err, db) => {
const { name } = req.params;
//res.status(200).send(name);
if (err) return next(err);
db.db('Test').collection(collection).findOne({ name: name }, (err2, result) => {
db.close();
if (err2) return next(err2);
res.status(200).send(result);
});
});
});
//To test the information sent to the database
server.post('/submit/', (req, res, next) => {
const { MONGO_URL } = req.webtaskContext.secrets;
// Do data sanitation here.
const model = req.body;
console.log(req.body);
//res.status(200).send(model);
MongoClient.connect(MONGO_URL, (err, db) => {
if (err) return next(err);
db.db('Test').collection(collection).insertOne(model, (err2, result) => {
db.close();
if (err2) return next(err2);
res.status(201).send(result);
});
});
});
server.post('/:name/:Job', (req, res, next) => {
const { MONGO_URL } = req.webtaskContext.secrets;
// Do data sanitation here.
const model = req.params;
//res.status(200).send(model);
MongoClient.connect(MONGO_URL, (err, db) => {
if (err) return next(err);
db.db('Test').collection(collection).insertOne(model, (err2, result) => {
db.close();
if (err2) return next(err2);
res.status(201).send(result);
});
});
});
module.exports = Webtask.fromExpress(server);