-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
128 lines (112 loc) · 2.79 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
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/cute', {
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 items in the show down: a title and a path to an image.
const itemSchema = new mongoose.Schema({
title: String,
path: String,
score: Number
});
itemSchema.virtual('id').get(function()
{
return this._id.toHexString();
});
// Create a model for items in the cute Ranking database
const Item = mongoose.model('Item', itemSchema);
app.listen(9001, () => console.log('Server listening on port 9001!'));
// Create a new item in the cute Ranking takes a title and a path to an image.
app.post('/api/items', async (req, res) => {
const item = new Item({
title: req.body.title,
path: req.body.path,
score: req.body.score,
});
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 Ranking
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 Ranking
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);
}
});