-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
196 lines (173 loc) · 5.49 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import { fileURLToPath } from 'url';
import { dirname } from 'path';
import express from 'express';
import fs from 'fs';
import path from 'path';
import { filterByDifficulty } from './public/filterByDifficulty.js';
const app = express();
const port = 3000;
const projectsFile = 'projects.json';
// Get the current file's directory path
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// Middleware to parse JSON request bodies
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// Serve static files
app.use(express.static('public'));
// Route to get a random project as HTML
app.get('/project', (req, res) => {
fs.readFile(projectsFile, 'utf8', (err, data) => {
if (err) {
console.error(err);
res.status(500).send('Internal Server Error');
} else {
let randomProject;
randomProject= filterByDifficulty(data);
// Convert the random project to an HTML string
const html = `
<head>
<meta charset="UTF-8">
<title>Random Idea</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<main>
<div class="content">
<button id="beginner">Beginner</button>
<h2>${randomProject.name}</h2>
<p>Description: ${randomProject.description}</p>
<p>Difficulty: ${randomProject.difficulty}</p>
<p>Tags: ${randomProject.tags}</p>
</div>
<div class="toggle-dark-mode">
<img src="moon.svg" onclick="toggleDarkMode()" alt="Darkmode toggle">
</div>
</main>
<footer>
<p>Created by <a href="https://github.com/arozx/ideas">arozx</a></p>
</footer>
<script src="script.js"></script>
</body>
`;
// Set the Content-Type header to indicate HTML response
res.setHeader('Content-Type', 'text/html');
// Send the HTML string as the response
res.send(html);
}
});
});
// Route to serve the projects as HTML
app.get('/projects', (req, res) => {
fs.readFile(projectsFile, (err, data) => {
if (err) {
console.error(err);
res.status(500).send('Internal Server Error');
} else {
const projects = JSON.parse(data);
const projectsHTML = projects
.map(
project => `
<div>
<h2>${project.name}</h2>
<p>${project.description}</p>
</div>
`
)
.join('');
const html = `
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Projects</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<header>
<nav>
<h1>Projects</h1>
</nav>
</header>
<main>
<div class="content">
<p>A list of projects</p>
${projectsHTML}
</div>
<div class="toggle-dark-mode">
<img src="moon.svg" onclick="toggleDarkMode()" alt="Darkmode toggle">
</div>
</main>
<footer>
<p>Created by <a href="https://github.com/arozx/ideas">arozx</a></p>
</footer>
<script src="script.js"></script>
</body>
</html>
`;
res.send(html);
}
});
});
// Route to handle adding a project
app.get('/add-project', (req, res) => {
const addProjectFilePath = path.join(__dirname, 'public', 'add-project.html');
fs.readFile(addProjectFilePath, 'utf8', (err, data) => {
if (err) {
console.error(err);
res.status(500).send('Internal Server Error');
} else {
res.setHeader('Content-Type', 'text/html');
res.send(data);
}
});
});
// Route to handle adding a project
app.post('/add-project', (req, res) => {
const { name, description, difficulty, tags } = req.body;
// Input validation
if (!name || !description || !difficulty || !tags) {
return res.status(400).send('All fields are required');
}
const maxNameLength = 50;
if (name.length > maxNameLength) {
return res.status(400).send(`Project name should be up to ${maxNameLength} characters long`);
}
const maxDescriptionLength = 200;
if (description.length > maxDescriptionLength) {
return res.status(400).send(`Project description should be up to ${maxDescriptionLength} characters long`);
}
const maxDifficultyLength = 12;
if (difficulty.length > maxDifficultyLength) {
return res.status(400).send(`Project difficulty should be up to ${maxDifficultyLength} characters long`);
}
const maxTagLength = 50;
if (tags.length > maxTagLength) {
return res.status(400).send(`Project tags should be up to ${maxTagLength} characters long`);
}
const newProject = {
name,
description,
difficulty,
tags,
};
fs.readFile(projectsFile, (err, data) => {
if (err) {
console.error(err);
res.status(500).send('Internal Server Error');
} else {
const projects = JSON.parse(data);
projects.push(newProject);
fs.writeFile(projectsFile, JSON.stringify(projects), (err) => {
if (err) {
console.error(err);
res.status(500).send('Internal Server Error');
} else {
// Instead of redirecting, return JSON response with the redirection URL
res.status(200).json({ redirectUrl: '/project' });
}
});
}
});
});
export default app;