-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
142 lines (117 loc) · 3.44 KB
/
server.js
File metadata and controls
142 lines (117 loc) · 3.44 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
import express from 'express';
import { connectToDb, getDb } from './db.js';
import { ObjectId } from 'mongodb';
import { Schema, model } from 'mongoose';
import { logger } from './middlewares/logger.js';
const projectSchema = new Schema({
author: { type: String, required: true },
dateCreated: { type: String, required: true },
lastModified: { type: String, required: true },
projectName: { type: String, required: true }
});
const Project = model('Project', projectSchema);
const app = express();
//db connection
let db
connectToDb((err) => {
if (!err) {
app.listen(4000, () => {
console.log('app listening on port 4000')
})
db = getDb()
}
})
app.set("view engine", "ejs")
app.use(logger)
app.use(express.static("public"))
app.use(express.urlencoded({ extended: true }))
app.get(['/', '/index'], (req, res) => {
res.render("index")
})
app.get('/about', (req, res) => {
res.render("about")
})
app.get('/login', (req, res) => {
res.render("login")
})
app.get('/editor', (req, res) => {
res.render("editor")
})
app.get('/projects', (req, res) => {
let projects = []
db.collection('projects')
.find()
.sort({author: 1})
.forEach(project => projects.push(project))
.then(() => {
res.status(200).render("projects", {projects: projects})
})
.catch(() => {
res.status(500).json({error: 'Could not fetch the documents'})
})
})
app.get('/user/:userId/editor/project/:projectName', (req, res) => {
res.render("editor", {projectName : req.params.projectName, userId : req.params.userId})
})
app.get('/newProject', (req, res) => {
res.render("newProject")
})
app.get('/editProject/:id', (req, res) => {
let id = new ObjectId(req.params.id)
db.collection('projects')
.findOne({_id: id})
.then(doc => {
res.render('editProject', {project: doc})
})
.catch(err => {
res.status(500).json({err: 'Could not fetch the project'})
})
})
app.post('/editProject/:id', (req, res) => {
const id = req.params.id;
const updated = {
projectName: req.body.title,
author: req.body.author,
lastModified: new Date().toDateString()
};
Project.findByIdAndUpdate(id, updated, { new: true })
.then(result => {
res.status(200).json(result);
})
.catch(err => {
res.status(500).json({ err: 'Could not update the project' });
});
});
app.post('/newProject', (req, res) => {
const date = new Date();
const project = new Project({
author: req.body.author,
dateCreated: date.toDateString(),
lastModified: date.toDateString(),
projectName: req.body.title
});
project.save()
.then(result => {
console.log("New project created:", result);
res.status(200).json(result);
})
.catch(err => {
console.error("Error creating project:", err);
res.status(500).json({ err: 'Could not create project' });
});
});
// POST route to delete a project
app.post('/delProject', (req, res) => {
const id = req.body.projectId;
Project.findByIdAndDelete(id)
.then(result => {
res.status(200).json(result);
console.log("Deleted project: " + id);
})
.catch(err => {
res.status(500).json({ err: 'Could not delete the project' });
});
});
app.get(['/:slug', '/:slug/*'], (req, res) => {
res.status(404).send(`Error 404 ${req.params.slug} not found`)
})