@@ -6,28 +6,48 @@ const path = require('path');
6
6
const app = express ( ) ;
7
7
const port = process . env . PORT || 3000 ;
8
8
9
+ // In-memory storage for wikis
9
10
let wikis = [
10
11
{ id : 1 , title : 'Node.js' , content : 'Node.js is a JavaScript runtime built on Chrome\'s V8 JavaScript engine.' , owner : 'krxzy_krxzy' } ,
11
12
{ id : 2 , title : 'JavaScript' , content : 'JavaScript is a programming language commonly used for web development.' , owner : 'myscratchedaccount' } ,
12
13
] ;
13
14
14
15
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)
17
18
19
+ // Authorized users for editing and deleting wikis
18
20
const authorizedUsers = [ 'kRxZy_kRxZy' , 'MyScratchedAccount' , 'mcgdj' ] ;
19
21
22
+ // Middleware to check if the user is authorized to edit or delete a wiki
20
23
const isAuthorized = ( username , wikiOwner ) => {
21
24
return username === wikiOwner || authorizedUsers . includes ( username ) ;
22
25
} ;
23
26
27
+ // API: Create a new wiki
24
28
app . post ( '/api/wikis' , ( req , res ) => {
25
29
const { title, content, owner } = req . body ;
26
30
const newWiki = { id : wikis . length + 1 , title, content, owner } ;
27
31
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
29
33
} ) ;
30
34
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
31
51
app . get ( '/wiki/:title' , ( req , res ) => {
32
52
const { title } = req . params ;
33
53
const wiki = wikis . find ( w => w . title . toLowerCase ( ) === title . toLowerCase ( ) ) ;
@@ -69,14 +89,15 @@ app.get('/wiki/:title', (req, res) => {
69
89
<p>${ wiki . content } </p>
70
90
<small>Author: ${ wiki . owner } </small>
71
91
<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>
74
94
</div>
75
95
</div>
76
96
</body>
77
97
</html>` ) ;
78
98
} ) ;
79
99
100
+ // Start the server
80
101
app . listen ( port , ( ) => {
81
102
console . log ( `Server is running on port ${ port } ` ) ;
82
103
} ) ;
0 commit comments