This is a simple Node.js and MongoDB project that demonstrates the use of Mongoose to define and interact with MongoDB models.
- Clone the repository
- Run
npm install
to install the dependencies
To use the project, follow these steps:
- Start the Node.js server by running
npm start
- Use a REST client, such as Postman, to send HTTP requests to the server
The User model represents a user in the system. It has the following properties:
name
(string, required) - The user's nameemail
(string, required, unique) - The user's email addresspassword
(string, required) - The user's passwordtotalLikes
(number, default 3) - The total number of likes the user has givennumPosts
(number, default 0) - The total number of posts the user has created
The Post model represents a post in the system. It has the following properties:
postId
(ObjectId, unique, default generated) - The post's IDuserId
(ObjectId, required, ref: 'User') - The ID of the user who created the postdata
(string, required) - The content of the postlikes
(array of objects) - An array of objects representing the users who have liked the post, and the timestamps when they liked ituserId
(ObjectId, required, ref: 'User') - The ID of the user who liked the posttimestamp
(Date, default: Date.now) - The timestamp when the user liked the post
numLikes
(number, default 0) - The total number of likes the post has received
express
mongoose
body-parser
Endpoint: POST /user
Request body: { name: string, // required email: string, // required, must be unique password: string // required }
Response: { _id: ObjectId, name: string, email: string, password: string, totalLikes: number, numPosts: number }
Endpoint: GET /user/:userId
Response: { _id: ObjectId, name: string, email: string, password: string, totalLikes: number, numPosts: number }
Endpoint: GET /post
Response: [{ _id: ObjectId, userId: ObjectId, data: string, likes: [{ userId: ObjectId, timestamp: Date }], numLikes: number }]
Endpoint: POST /post
Request body: { userId: ObjectId, // required data: string // required }
Response: { _id: ObjectId, userId: ObjectId, data: string, likes: [], numLikes: number }
Endpoint: POST /like
Request body: { postId: ObjectId, // required userId: ObjectId // required }
Response: { message: string }
Endpoint: GET /famous
Response: [{ _id: ObjectId, userId: ObjectId, data: string, likes: [{ userId: ObjectId, timestamp: Date }], numLikes: number }]