-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
executable file
·139 lines (123 loc) · 3.09 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/home/ec2-user/.nvm/versions/node/v14.2.0/bin/ node
const express = require('express');
const bodyParser = require("body-parser");
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(express.static('public'));
const mongoose = require('mongoose');
// connect to the database
mongoose.connect('mongodb://localhost:27017/greenhouse', {
useNewUrlParser: true
});
// Configure multer so that it will upload to '/public/images'
const multer = require('multer')
const upload = multer({
dest: './public/images/',
limits: {
fileSize: 10000000
}
});
// Create a scheme for snapshot items in the greenhouse
const itemSchema = new mongoose.Schema({
title: String,
path: String,
humid: Number,
temp: Number,
moisture: Boolean,
intLight: Boolean,
extLight: Boolean
});
itemSchema.virtual('id').get(function()
{
return this._id.toHexString();
});
// Create a model for items in the greenhouse database
const Item = mongoose.model('Item', itemSchema);
app.listen(9000, () => console.log('Server listening on port 9000 !'));
// Create a new snpashot item in the greenohseu DB
app.post('/api/items', async (req, res) => {
console.log("Request Received");
help = req.body.title
const item = new Item({
title: req.body.title,
path: req.body.imgPath,
temp: (((req.body.temp) *(9/5))+32).toFixed(2),
humid: req.body.humid,
intLight: req.body.intLight,
extLight: req.body.extLight,
moisture: req.body.moisture
});
try {
await item.save();
res.send(item);
} catch (error) {
console.log(error);
res.sendStatus(500);
}
});
// Upload an image and keeps a path to the upladed imnage for future reference
app.post('/api/photos', upload.single('photo'), async (req, res) => {
// Just a safety check
if (!req.file) {
return res.sendStatus(400);
}
res.send({
path: "/images/" + req.file.filename
});
});
// Get a list of all of the competertiors in the cute showdown
app.get('/api/items', async (req, res) => {
try {
let items = await Item.find();
res.send(items);
} catch (error) {
console.log(error);
res.sendStatus(500);
}
});
// remove one of the items from the cute showdown
app.delete('/api/items/:id', async (req, res) => {
try {
await Item.deleteOne(
{
_id: req.params.id
});
res.sendStatus(200);
} catch (error) {
console.log(error);
res.sendStatus(500);
}
});
// edit one of the item's score in the database
app.put('/api/items/:id', async (req, res) => {
try {
let item = await Item.findOne(
{
_id: req.params.id
});
item.score = req.body.score;
item.save();
res.sendStatus(200);
} catch (error) {
console.log(error);
res.sendStatus(500);
}
});
// edit one of the item's name in the database
app.put('/api/change/:id', async (req, res) => {
try {
let item = await Item.findOne(
{
_id: req.params.id
});
item.title = req.body.title;
item.save();
res.sendStatus(200);
} catch (error) {
console.log(error);
res.sendStatus(500);
}
});