|
1 | 1 | const express = require('express');
|
2 | 2 | const bodyParser = require('body-parser');
|
3 |
| -const cors = require('cors'); // Import CORS |
| 3 | +const cors = require('cors'); |
| 4 | +const path = require('path'); |
4 | 5 |
|
5 | 6 | const app = express();
|
6 |
| -const port = process.env.PORT || 3000; // Port for Render to listen on |
| 7 | +const port = process.env.PORT || 3000; |
7 | 8 |
|
8 |
| -// In-memory storage for wikis |
9 | 9 | let wikis = [
|
10 | 10 | { id: 1, title: 'Node.js', content: 'Node.js is a JavaScript runtime built on Chrome\'s V8 JavaScript engine.', owner: 'krxzy_krxzy' },
|
11 | 11 | { id: 2, title: 'JavaScript', content: 'JavaScript is a programming language commonly used for web development.', owner: 'myscratchedaccount' },
|
12 | 12 | ];
|
13 | 13 |
|
14 | 14 | app.use(bodyParser.json());
|
15 |
| -app.use(cors()); // Enable CORS for all requests |
| 15 | +app.use(cors()); |
| 16 | +app.use(express.static('public')); |
16 | 17 |
|
17 | 18 | const authorizedUsers = ['kRxZy_kRxZy', 'MyScratchedAccount', 'mcgdj'];
|
18 | 19 |
|
19 |
| -// Middleware to check if user is authorized to edit or delete |
20 | 20 | const isAuthorized = (username, wikiOwner) => {
|
21 | 21 | return username === wikiOwner || authorizedUsers.includes(username);
|
22 | 22 | };
|
23 | 23 |
|
24 |
| -// Serve static files (public assets) |
25 |
| -app.use(express.static('public')); |
26 |
| - |
27 |
| -// Create a new wiki |
28 | 24 | app.post('/api/wikis', (req, res) => {
|
29 | 25 | const { title, content, owner } = req.body;
|
30 | 26 | const newWiki = { id: wikis.length + 1, title, content, owner };
|
31 | 27 | wikis.push(newWiki);
|
32 |
| - res.status(201).json(newWiki); |
| 28 | + res.status(201).send(`Wiki Created! <a href="/wiki/${encodeURIComponent(title)}">View Wiki</a>`); |
33 | 29 | });
|
34 | 30 |
|
35 |
| -// Edit an existing wiki |
36 |
| -app.put('/api/wikis/:id', (req, res) => { |
37 |
| - const { id } = req.params; |
38 |
| - const { title, content, owner } = req.body; |
| 31 | +app.get('/wiki/:title', (req, res) => { |
| 32 | + const { title } = req.params; |
| 33 | + const wiki = wikis.find(w => w.title.toLowerCase() === title.toLowerCase()); |
39 | 34 |
|
40 |
| - const wiki = wikis.find(wiki => wiki.id === parseInt(id)); |
41 |
| - if (wiki && isAuthorized(owner, wiki.owner)) { |
42 |
| - wiki.title = title; |
43 |
| - wiki.content = content; |
44 |
| - res.json(wiki); |
45 |
| - } else { |
46 |
| - res.status(403).send('Not authorized or wiki not found'); |
| 35 | + if (!wiki) { |
| 36 | + return res.status(404).send('Wiki not found'); |
47 | 37 | }
|
48 |
| -}); |
49 |
| - |
50 |
| -// Delete a wiki |
51 |
| -app.delete('/api/wikis/:id', (req, res) => { |
52 |
| - const { id } = req.params; |
53 |
| - const { owner } = req.body; |
54 |
| - |
55 |
| - const wikiIndex = wikis.findIndex(wiki => wiki.id === parseInt(id)); |
56 |
| - if (wikiIndex !== -1 && isAuthorized(owner, wikis[wikiIndex].owner)) { |
57 |
| - wikis.splice(wikiIndex, 1); |
58 |
| - res.status(200).send('Wiki deleted'); |
59 |
| - } else { |
60 |
| - res.status(403).send('Not authorized or wiki not found'); |
61 |
| - } |
62 |
| -}); |
63 | 38 |
|
64 |
| -// Get all wikis |
65 |
| -app.get('/api/wikis', (req, res) => { |
66 |
| - res.json(wikis); |
| 39 | + res.send(`<!DOCTYPE html> |
| 40 | +<html lang="en"> |
| 41 | +<head> |
| 42 | + <meta charset="UTF-8"> |
| 43 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 44 | + <title>${wiki.title} - Wiki</title> |
| 45 | + <style> |
| 46 | + * { margin: 0; padding: 0; box-sizing: border-box; } |
| 47 | + body { font-family: 'Arial', sans-serif; background: #fff4f4; color: #333; text-align: center; } |
| 48 | + .navbar { display: flex; justify-content: center; align-items: center; background: #ff4d4d; padding: 15px 20px; border-radius: 10px; gap: 15px; } |
| 49 | + .navbar a { color: white; text-decoration: none; font-size: 1.2rem; padding: 10px 15px; border-radius: 5px; transition: background 0.3s; } |
| 50 | + .navbar a:hover { background: #ff1a1a; } |
| 51 | + .wiki-content { max-width: 1000px; margin: 40px auto; background: #ffe6e6; padding: 40px; border-radius: 10px; text-align: left; } |
| 52 | + .wiki-content h2 { color: #e60000; font-size: 2.5rem; margin-bottom: 20px; text-align: center; } |
| 53 | + .wiki-content p { font-size: 1.5rem; color: #333; line-height: 1.6; text-align: center; } |
| 54 | + .button-container { margin-top: 20px; display: flex; gap: 10px; justify-content: center; } |
| 55 | + .edit-button, .report-button { display: inline-block; padding: 10px 20px; font-size: 1.2rem; border: none; border-radius: 5px; cursor: pointer; transition: background 0.3s; text-decoration: none; color: white; } |
| 56 | + .edit-button { background: #ffcc00; } |
| 57 | + .edit-button:hover { background: #ffaa00; } |
| 58 | + .report-button { background: #ff4d4d; } |
| 59 | + .report-button:hover { background: #ff1a1a; } |
| 60 | + </style> |
| 61 | +</head> |
| 62 | +<body> |
| 63 | + <div class="navbar"> |
| 64 | + <a href="/index.html">Home</a> |
| 65 | + <a href="/Wiki/sitemaplinks.html">Create Wiki</a> |
| 66 | + </div> |
| 67 | + <div class="wiki-content"> |
| 68 | + <h2>${wiki.title}</h2> |
| 69 | + <p>${wiki.content}</p> |
| 70 | + <small>Author: ${wiki.owner}</small> |
| 71 | + <div class="button-container"> |
| 72 | + <a href="/Wiki/edit?edit=${encodeURIComponent(wiki.title)}" class="edit-button">Edit Wiki</a> |
| 73 | + <a href="/Wiki/report.html" class="report-button">Report</a> |
| 74 | + </div> |
| 75 | + </div> |
| 76 | +</body> |
| 77 | +</html>`); |
67 | 78 | });
|
68 | 79 |
|
69 |
| -// Start the server |
70 | 80 | app.listen(port, () => {
|
71 | 81 | console.log(`Server is running on port ${port}`);
|
72 | 82 | });
|
0 commit comments