Skip to content

Commit da8f8ea

Browse files
authored
Update server.js
1 parent 13d9c37 commit da8f8ea

File tree

1 file changed

+50
-40
lines changed

1 file changed

+50
-40
lines changed

src/npm/server.js

Lines changed: 50 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,82 @@
11
const express = require('express');
22
const bodyParser = require('body-parser');
3-
const cors = require('cors'); // Import CORS
3+
const cors = require('cors');
4+
const path = require('path');
45

56
const app = express();
6-
const port = process.env.PORT || 3000; // Port for Render to listen on
7+
const port = process.env.PORT || 3000;
78

8-
// In-memory storage for wikis
99
let wikis = [
1010
{ id: 1, title: 'Node.js', content: 'Node.js is a JavaScript runtime built on Chrome\'s V8 JavaScript engine.', owner: 'krxzy_krxzy' },
1111
{ id: 2, title: 'JavaScript', content: 'JavaScript is a programming language commonly used for web development.', owner: 'myscratchedaccount' },
1212
];
1313

1414
app.use(bodyParser.json());
15-
app.use(cors()); // Enable CORS for all requests
15+
app.use(cors());
16+
app.use(express.static('public'));
1617

1718
const authorizedUsers = ['kRxZy_kRxZy', 'MyScratchedAccount', 'mcgdj'];
1819

19-
// Middleware to check if user is authorized to edit or delete
2020
const isAuthorized = (username, wikiOwner) => {
2121
return username === wikiOwner || authorizedUsers.includes(username);
2222
};
2323

24-
// Serve static files (public assets)
25-
app.use(express.static('public'));
26-
27-
// Create a new wiki
2824
app.post('/api/wikis', (req, res) => {
2925
const { title, content, owner } = req.body;
3026
const newWiki = { id: wikis.length + 1, title, content, owner };
3127
wikis.push(newWiki);
32-
res.status(201).json(newWiki);
28+
res.status(201).send(`Wiki Created! <a href="/wiki/${encodeURIComponent(title)}">View Wiki</a>`);
3329
});
3430

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());
3934

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');
4737
}
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-
});
6338

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>`);
6778
});
6879

69-
// Start the server
7080
app.listen(port, () => {
7181
console.log(`Server is running on port ${port}`);
7282
});

0 commit comments

Comments
 (0)