Skip to content

Commit

Permalink
get users for sidebar
Browse files Browse the repository at this point in the history
  • Loading branch information
u0509421 committed Jan 10, 2025
1 parent 7d15ddf commit cc9358c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
16 changes: 16 additions & 0 deletions backend/controllers/user.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import User from "../models/user.model.js";

export const getUsersForSidebar = async (req, res) => {
try {
const loggedInUserId = req.user._id;

const filteredUsers = await User.find({
_id: { $ne: loggedInUserId },
}).select("-password");

res.status(200).json(filteredUsers);
} catch (error) {
console.log("error in getUsersForSidebar controller", error.message);
res.status(500).json({ error: "Internal server error" });
}
};
9 changes: 9 additions & 0 deletions backend/routes/user.routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import express from "express";
import protectRoute from "../middleware/protectRoute.js";
import { getUsersForSidebar } from "../controllers/user.controller.js";

const router = express.Router();

router.get("/", protectRoute, getUsersForSidebar);

export default router;
6 changes: 5 additions & 1 deletion backend/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import express from "express";
import dotenv from "dotenv";
import cookieParser from "cookie-parser";

// file imports
// routes imports
import authRoutes from "./routes/auth.routes.js";
import messageRoutes from "./routes/message.routes.js";
import userRoutes from "./routes/user.routes.js";

import connectToMongoDB from "./db/connectToMongoDB.js";

const app = express();
Expand All @@ -14,11 +16,13 @@ const PORT = process.env.PORT || 3000;
dotenv.config();

// middleware
// cookieParser 要在express.json之前使用!!!!否则protectRoute那里无法拿到cookie
app.use(cookieParser());
app.use(express.json()); // for parsing application/json (from req.body)

app.use("/api/auth", authRoutes);
app.use("/api/messages", messageRoutes);
app.use("/api/users", userRoutes);

// app.get("/", (req, res) => {
// // root route http://localhost:3000
Expand Down

0 comments on commit cc9358c

Please sign in to comment.