-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
146 lines (125 loc) · 4.14 KB
/
Copy pathserver.js
File metadata and controls
146 lines (125 loc) · 4.14 KB
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
140
141
142
143
144
145
146
require('dotenv').config();
const express = require('express');
const mongoose = require('mongoose');
const session = require('express-session');
const MongoStore = require('connect-mongo');
const path = require('path');
const noteRoutes = require('./web/routes/noteRoutes');
const authRoutes = require('./web/routes/authRoutes');
const Note = require('./web/models/Note');
const { requireAuth, requireGuest } = require('./web/middleware/auth');
const app = express();
const PORT = process.env.PORT || 3000;
mongoose.connect(process.env.MONGODB_URI || 'mongodb://localhost:27017/notemanager')
.then(() => console.log('MongoDB Connected'))
.catch(err => console.error('MongoDB Error:', err));
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'web/views'));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, 'web/public')));
app.use(session({
secret: 'notemanager-secret-key-2026',
resave: false,
saveUninitialized: false,
store: MongoStore.create({
mongoUrl: process.env.MONGODB_URI || 'mongodb://localhost:27017/notemanager'
}),
cookie: {
maxAge: 1000 * 60 * 60 * 24
}
}));
app.use((req, res, next) => {
res.locals.userId = req.session.userId;
res.locals.userName = req.session.userName;
next();
});
app.get('/', requireGuest, (req, res) => {
res.render('landing', { title: 'NoteManager - Login' });
});
app.get('/dashboard', requireAuth, async (req, res) => {
try {
const allNotes = await Note.find({ owner: req.session.userId }).sort({ createdAt: -1 });
const stats = {
total: allNotes.length,
pending: allNotes.filter(n => n.status === 'pending').length,
inProgress: allNotes.filter(n => n.status === 'in-progress').length,
completed: allNotes.filter(n => n.status === 'completed').length
};
const filter = req.query.filter || 'all';
const sort = req.query.sort || 'newest';
let notes = allNotes;
if (filter !== 'all') {
notes = allNotes.filter(n => n.status === filter);
}
if (sort === 'oldest') {
notes = notes.reverse();
} else if (sort === 'status') {
const statusOrder = { 'pending': 1, 'in-progress': 2, 'completed': 3 };
notes = notes.sort((a, b) => statusOrder[a.status] - statusOrder[b.status]);
}
res.render('dashboard', {
title: 'Dashboard',
notes,
stats,
filter,
sort
});
} catch (error) {
res.status(500).send('Error loading dashboard');
}
});
app.post('/add', requireAuth, async (req, res) => {
try {
const { title, body, status, priority, category } = req.body;
const note = new Note({
title,
body,
status: status || 'pending',
priority: priority || 'medium',
category: category || 'general',
owner: req.session.userId
});
await note.save();
res.redirect('/dashboard');
} catch (error) {
res.status(400).send('Error adding note');
}
});
app.get('/edit/:id', requireAuth, async (req, res) => {
try {
const note = await Note.findOne({ _id: req.params.id, owner: req.session.userId });
if (!note) {
return res.status(404).send('Note not found');
}
res.render('edit', { title: 'Edit Note', note });
} catch (error) {
res.status(500).send('Error loading note');
}
});
app.post('/update/:id', requireAuth, async (req, res) => {
try {
const { title, body, status, priority, category } = req.body;
await Note.findOneAndUpdate(
{ _id: req.params.id, owner: req.session.userId },
{ title, body, status, priority, category }
);
res.redirect('/dashboard');
} catch (error) {
res.status(400).send('Error updating note');
}
});
app.post('/delete/:id', requireAuth, async (req, res) => {
try {
await Note.findOneAndDelete({ _id: req.params.id, owner: req.session.userId });
res.redirect('/dashboard');
} catch (error) {
res.status(400).send('Error deleting note');
}
});
app.use('/auth', authRoutes);
app.use('/api', noteRoutes);
app.listen(PORT, () => {
console.log(`NoteManager running on http://localhost:${PORT}`);
console.log(`CLI: node cli/app.js --help`);
});