forked from RishikaReddy123/mern-blog
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
83 lines (70 loc) · 1.89 KB
/
server.js
File metadata and controls
83 lines (70 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const express = require("express");
const app = express();
const mongoose = require("mongoose");
const PORT = process.env.PORT || 8000;
require("dotenv").config();
if (!process.env.MONGO_URI) {
console.error("MONGO_URI is not defined in the .env file");
process.exit(1);
}
mongoose
.connect(process.env.MONGO_URI)
.then(() => console.log("Connected to MongoDB"))
.catch((err) => console.log("Failed to connect to MongoDB", err));
app.use(express.json({ extended: false }));
const articleSchema = new mongoose.Schema({
name: {
type: String,
required: true,
unique: true,
},
comments: [
{
username: {
type: String,
required: true,
},
text: {
type: String,
required: true,
},
},
],
});
const Article = mongoose.model("Article", articleSchema);
app.get("/", (req, res) => {
res.send("Welcome");
});
app.get("/api/articles/:name", async (req, res) => {
const articleName = req.params.name;
try {
const article = await Article.findOne({ name: articleName });
if (!article) {
return res.status(404).json({ message: "Article not found" });
}
res.status(200).json(article);
} catch (err) {
console.error("Error fetching article:", err);
res.status(500).json({ message: "Server Error" });
}
});
app.post("/api/articles/:name/add-comments", async (req, res) => {
const { username, text } = req.body;
const articleName = req.params.name;
try {
let article = await Article.findOne({ name: articleName });
if (!article) {
article = new Article({
name: articleName,
comments: [],
});
}
article.comments.push({ username, text });
await article.save();
res.status(200).send(article);
} catch (err) {
console.error(err);
res.status(500).send("Server Error!");
}
});
app.listen(PORT, () => console.log(`Server is listening on ${PORT}`));