Skip to content

Commit d61d5ca

Browse files
authored
Update server.js
1 parent 6ee9caa commit d61d5ca

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

src/npm/server.js

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,48 @@ const path = require('path');
66
const app = express();
77
const port = process.env.PORT || 3000;
88

9+
// In-memory storage for wikis
910
let wikis = [
1011
{ id: 1, title: 'Node.js', content: 'Node.js is a JavaScript runtime built on Chrome\'s V8 JavaScript engine.', owner: 'krxzy_krxzy' },
1112
{ id: 2, title: 'JavaScript', content: 'JavaScript is a programming language commonly used for web development.', owner: 'myscratchedaccount' },
1213
];
1314

1415
app.use(bodyParser.json());
15-
app.use(cors());
16-
app.use(express.static('public'));
16+
app.use(cors()); // Enable CORS for all requests
17+
app.use(express.static('public')); // Serve static files (e.g., images, CSS)
1718

19+
// Authorized users for editing and deleting wikis
1820
const authorizedUsers = ['kRxZy_kRxZy', 'MyScratchedAccount', 'mcgdj'];
1921

22+
// Middleware to check if the user is authorized to edit or delete a wiki
2023
const isAuthorized = (username, wikiOwner) => {
2124
return username === wikiOwner || authorizedUsers.includes(username);
2225
};
2326

27+
// API: Create a new wiki
2428
app.post('/api/wikis', (req, res) => {
2529
const { title, content, owner } = req.body;
2630
const newWiki = { id: wikis.length + 1, title, content, owner };
2731
wikis.push(newWiki);
28-
res.status(201).send(`Wiki Created! <a href="/wiki/${encodeURIComponent(title)}">View Wiki</a>`);
32+
res.status(201).json(newWiki); // Return the created wiki as JSON
2933
});
3034

35+
// API: Get all wikis
36+
app.get('/api/wikis', (req, res) => {
37+
res.json(wikis); // Return all wikis as JSON
38+
});
39+
40+
// API: Get a specific wiki by ID
41+
app.get('/api/wikis/:id', (req, res) => {
42+
const { id } = req.params;
43+
const wiki = wikis.find(wiki => wiki.id === parseInt(id));
44+
if (!wiki) {
45+
return res.status(404).json({ error: 'Wiki not found' });
46+
}
47+
res.json(wiki); // Return the specific wiki as JSON
48+
});
49+
50+
// Serve HTML page for a specific wiki title
3151
app.get('/wiki/:title', (req, res) => {
3252
const { title } = req.params;
3353
const wiki = wikis.find(w => w.title.toLowerCase() === title.toLowerCase());
@@ -69,14 +89,15 @@ app.get('/wiki/:title', (req, res) => {
6989
<p>${wiki.content}</p>
7090
<small>Author: ${wiki.owner}</small>
7191
<div class="button-container">
72-
<a href="scratch-coding-hut.github.io/Wiki/edit?edit=${encodeURIComponent(wiki.title)}" class="edit-button">Edit Wiki</a>
73-
<a href="scratch-coding-hut.github.io/Wiki/report.html?wiki=${encodeURIComponent(wiki.title)}" class="report-button">Report</a>
92+
<a href="https://scratch-coding-hut.github.io/Wiki/edit?edit=${encodeURIComponent(wiki.title)}" class="edit-button">Edit Wiki</a>
93+
<a href="https://scratch-coding-hut.github.io/Wiki/report.html?wiki=${encodeURIComponent(wiki.title)}" class="report-button">Report</a>
7494
</div>
7595
</div>
7696
</body>
7797
</html>`);
7898
});
7999

100+
// Start the server
80101
app.listen(port, () => {
81102
console.log(`Server is running on port ${port}`);
82103
});

0 commit comments

Comments
 (0)