-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
127 lines (106 loc) · 3.18 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
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
const express = require('express');
const mongoose = require('mongoose');
const dotenv = require('dotenv');
const multer = require('multer');
const path = require('path');
const session = require('express-session');
const passport = require('passport');
const fs = require('fs');
const favicon = require('serve-favicon');
dotenv.config();
const app = express();
mongoose.connect(process.env.MONGO_URI);
app.set('view engine', 'ejs');
// Middleware
app.use(express.static('public'));
app.use(express.urlencoded({ extended: true }));
app.use(favicon(path.join(__dirname, 'public', 'images', 'favicon.ico')));
// Set storage engine for multer
const storage = multer.diskStorage({
destination: './public/uploads/',
filename: function(req, file, cb){
cb(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname));
}
});
const upload = multer({ storage: storage });
// Define schema for cool stuff
const CoolStuffSchema = new mongoose.Schema({
title: String,
description: String,
imagePath: String,
location: {
lat: Number,
lng: Number
}
});
const CoolStuff = mongoose.model('CoolStuff', CoolStuffSchema);
// Routes
app.get('/', async (req, res) => {
const coolItems = await CoolStuff.find();
res.render('index', { coolItems });
});
app.get('/add', (req, res) => {
res.render('add');
});
app.post('/add', upload.single('image'), async (req, res) => {
const newCoolItem = new CoolStuff({
title: req.body.title,
description: req.body.description,
imagePath: '/uploads/' + req.file.filename,
location: {
lat: req.body.lat,
lng: req.body.lng
}
});
await newCoolItem.save();
res.redirect('/');
});
app.get('/confirmRemoveAll', (req, res) => {
res.render('confirmRemoveAll');
});
app.post('/removeAll', async (req, res) => {
const adminPassword = req.body.adminPassword;
const correctPassword = 'somepassword';
if (adminPassword !== correctPassword) {
return res.status(403).send('Incorrect password.');
}
try {
const items = await CoolStuff.find();
for (const item of items) {
if (item.imagePath) {
const imagePath = path.join(__dirname, 'public', item.imagePath);
fs.unlink(imagePath, (err) => {
if (err) {
console.error(`Error deleting image file for ${item.title}:`, err);
}
});
}
}
await CoolStuff.deleteMany({});
res.redirect('/');
} catch (error) {
console.error('Error removing all items:', error);
res.status(500).send('Error removing all items.');
}
});
app.post('/remove/:id', async (req, res) => {
try {
const item = await CoolStuff.findById(req.params.id);
if (item && item.imagePath) {
const imagePath = path.join(__dirname, 'public', item.imagePath);
fs.unlink(imagePath, (err) => {
if (err) {
console.error(`Error deleting image file for ${item.title}:`, err);
}
});
}
await CoolStuff.findByIdAndDelete(req.params.id);
res.redirect('/');
} catch (error) {
console.error('Error deleting item:', error);
res.status(500).send('Error deleting item');
}
});
app.listen(process.env.PORT || 3000, () => {
console.log('Server started on port 3000');
});