Skip to content

Commit

Permalink
connecting backend with frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashishsharma1998 committed Apr 29, 2023
1 parent b0e69ba commit 07d243d
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 6 deletions.
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@ const mongoose = require("mongoose");
const dotenv = require("dotenv");
const morgan = require("morgan");
const helmet = require("helmet");
const cors = require("cors");
const userApiRoutes = require("./routes/users");
const authApiRoutes = require("./routes/auth");
const postApiRoutes = require("./routes/post");
const app = express();
dotenv.config();

app.use(cors());
app.use(express.json());
app.use(morgan("common"));
app.use("/api/user", userApiRoutes);
app.use("/api/auth", authApiRoutes);
app.use("/api/posts", postApiRoutes);

app.listen(3000, async () => {
console.log("server is runing at port 3000");
app.listen(3001, async () => {
console.log("server is runing at port 3001");
await mongoose.connect(process.env.MONGODB_URL);
});
22 changes: 22 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"license": "ISC",
"dependencies": {
"bcrypt": "^5.1.0",
"cors": "^2.8.5",
"dotenv": "^16.0.3",
"express": "^4.18.2",
"helmet": "^6.0.1",
Expand Down
16 changes: 14 additions & 2 deletions routes/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ router.get("/:id", async (req, res) => {
});

//get timeline posts
router.get("/timeline/all", async (req, res) => {
router.get("/timeline/:userId", async (req, res) => {
try {
const currentUser = await User.findById(req.body.userId);
const currentUser = await User.findById(req.params.userId);
const userPost = await Post.find({ userId: currentUser._id });
const frientPost = await Promise.all(
currentUser.followings.map((friendId) => {
Expand All @@ -85,4 +85,16 @@ router.get("/timeline/all", async (req, res) => {
return res.status(500).json(error);
}
});

//get all users posts
router.get("/profile/:username", async (req, res) => {
try {
const user = await User.findOne({ username: req.params.username });
const posts = await Post.find({ userId: user._id });
return res.status(200).json(posts);
} catch (error) {
return res.status(500).json(error);
}
});

module.exports = router;
9 changes: 7 additions & 2 deletions routes/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,14 @@ router.delete("/:id", async (req, res) => {
});

//get a user
router.get("/:id", async (req, res) => {
router.get("/", async (req, res) => {
try {
const user = await User.findById(req.params.id);
const userId = req.query.userId;
const username = req.query.username;

const user = userId
? await User.findById(userId)
: await User.findOne({ username: username });
const { password, createdAt, updatedAt, ...other } = user._doc;
return res.status(200).json(other);
} catch (error) {
Expand Down

0 comments on commit 07d243d

Please sign in to comment.