@lexho111/plainblog 0.5.25 → 0.5.27
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -0
- package/package.json +1 -1
- package/api-server.js +0 -70
package/README.md
CHANGED
|
@@ -89,6 +89,12 @@ The API should respond to GET-Requests (*http://example.com:5432/blog*) with jso
|
|
|
89
89
|
}
|
|
90
90
|
```
|
|
91
91
|
|
|
92
|
+
Post requests should look like:
|
|
93
|
+
|
|
94
|
+
```
|
|
95
|
+
curl -X POST http://example.com:5432/blog -H "Content-Type: application/json" -d "{\"title\": \"My new Article.\", \"content\": \"This is the content.\"}"
|
|
96
|
+
```
|
|
97
|
+
|
|
92
98
|
### provide custom style sheets
|
|
93
99
|
|
|
94
100
|
```
|
package/package.json
CHANGED
package/api-server.js
DELETED
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
import http from "http";
|
|
2
|
-
|
|
3
|
-
const PORT = 5432;
|
|
4
|
-
|
|
5
|
-
// In-memory data store
|
|
6
|
-
const blogData = {
|
|
7
|
-
title: "My Remote Blog",
|
|
8
|
-
articles: [
|
|
9
|
-
{
|
|
10
|
-
title: "Welcome to the API Server",
|
|
11
|
-
content: "This content is served from a separate API server.",
|
|
12
|
-
createdAt: new Date().toISOString(),
|
|
13
|
-
},
|
|
14
|
-
],
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
const server = http.createServer((req, res) => {
|
|
18
|
-
// Set CORS headers to allow requests from the blog application
|
|
19
|
-
res.setHeader("Access-Control-Allow-Origin", "*");
|
|
20
|
-
res.setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
|
|
21
|
-
res.setHeader("Access-Control-Allow-Headers", "Content-Type");
|
|
22
|
-
|
|
23
|
-
// Handle preflight requests
|
|
24
|
-
if (req.method === "OPTIONS") {
|
|
25
|
-
res.writeHead(204);
|
|
26
|
-
res.end();
|
|
27
|
-
return;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
const url = new URL(req.url, `http://${req.headers.host}`);
|
|
31
|
-
|
|
32
|
-
if (url.pathname === "/blog") {
|
|
33
|
-
if (req.method === "GET") {
|
|
34
|
-
res.writeHead(200, { "Content-Type": "application/json" });
|
|
35
|
-
res.end(JSON.stringify(blogData));
|
|
36
|
-
} else if (req.method === "POST") {
|
|
37
|
-
let body = "";
|
|
38
|
-
req.on("data", (chunk) => (body += chunk.toString()));
|
|
39
|
-
req.on("end", () => {
|
|
40
|
-
try {
|
|
41
|
-
const newArticle = JSON.parse(body);
|
|
42
|
-
if (!newArticle.createdAt) {
|
|
43
|
-
newArticle.createdAt = new Date().toISOString();
|
|
44
|
-
}
|
|
45
|
-
// Add the new article to the beginning of the list
|
|
46
|
-
blogData.articles.unshift(newArticle);
|
|
47
|
-
|
|
48
|
-
console.log("New article received:", newArticle.title);
|
|
49
|
-
|
|
50
|
-
res.writeHead(201, { "Content-Type": "application/json" });
|
|
51
|
-
res.end(JSON.stringify(newArticle));
|
|
52
|
-
} catch (error) {
|
|
53
|
-
console.error("Error parsing JSON:", error);
|
|
54
|
-
res.writeHead(400, { "Content-Type": "application/json" });
|
|
55
|
-
res.end(JSON.stringify({ error: "Invalid JSON" }));
|
|
56
|
-
}
|
|
57
|
-
});
|
|
58
|
-
} else {
|
|
59
|
-
res.writeHead(405); // Method Not Allowed
|
|
60
|
-
res.end();
|
|
61
|
-
}
|
|
62
|
-
} else {
|
|
63
|
-
res.writeHead(404); // Not Found
|
|
64
|
-
res.end();
|
|
65
|
-
}
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
server.listen(PORT, () => {
|
|
69
|
-
console.log(`API Server running at http://localhost:${PORT}/blog`);
|
|
70
|
-
});
|