Backend API similar to Stackoverflow.
Report Bug
Table of Contents
Backend Rest Api that includes basic functionalities that simple Q&A websites similar Stackoverflow provides.
To run locally with node:
- Clone this repo
npm install
to install all required dependencies- Create MongoDb Cluster
- Get Connection MongoDb URL
- Set environment variables in
./config/env/config.env
npm run dev
to start the local server
If you want, You can run node dummy-generator.js --import
to load dummy data to database
Specifications
-
List all questions
- Paginate and Limit number of Questions
- Sorting Questions By Most-Answered, Most-Liked or More Recent(Default)
- Searching Questions By Title
- Population User Of The Question
-
Get a single question with their answers
Private Operations
- Ask (Create) a New Question
- Authenticated users only (Logged In Users)
- Field validation
- Edit a Question
- Owner User Only
- Field Validation
- Delete a Question
- Owner User Only
- Like a Question
- Authenticated user only
- Only 1 Like Per User
- Undo Like a Question
- Authenticated user only
- Only Applicable To Question That Liked Before
- Get All Answers by Question Id
- Get Single Answer By Answer Id
-
Add (Create) a New Answer To Question
- Authenticated users only (Logged In Users)
- Field validation
-
Edit a Answer
- Owner User Only
- Field Validation
-
Delete a Answer
- Owner User Only
-
Like a Answer
- Authenticated user only
- Only 1 Like Per User
-
Undo Like a Answer
- Authenticated user only
- Only Applicable To Answer That Liked Before
- List all Users
- Paginate and Limit number of Users
- Search By name
- Get User Profile
- Block A User
- Delete A User
Requests are authenticated using the Authorization
header and value Bearer: {{token}}
. with a valid JWT.
- Authentication Strategy : JWT and Cookie
- JWT and Cookie Expiration : 30 Minutes For Testing Api
- Registration
- User can register as a "Admin" or simply "User"
- Password Hash
- Token includes : "id" and "name"
- Token Are Stored In Cookie
- Login
- User can login with "email" and "password"
- Everytime a user login, new Token are sent to to client and stored in cookie.
- Logout
- Token set to null in cookie.
- Forgot Password
- Reset Password Token send to client via email.
- This token expires in 1 hour.
- Reset Password
- Reset Password Token can be used in 1 hour.
- User can set a new password using this token.
- Update User Details (Bio)
- Users can add their bio details when logged in.
- User Profile
- Users can view their personal information after they login.
- Profile Photo Upload
- Users can upload an avatar for their profile.
- name
- type : String
- required : true
- Validation : Please provide a name
- email
- type : String
- required : true
- unique : true
- Validation with Regex : Please provide a valid email
- role
- type : String
- enum : user,admin
- default : user
- password
- type : String
- required : true
- minlength : 6
- Validation : Please provide a password
- createdAt
- type : String
- default : Date.now
- title
- type : String
- about
- type : String
- website
- type : String
- place
- type : String
- profile_image
- type : String
- blocked
- type : Boolean
- default : false
- resetPasswordToken
- type : String
- resetPasswordExpire
- type : Date
- title
- type : String
- required : true
- Validation : Please provide a title
- minLength : 10
- unique : true
- content
- type : String
- required : true
- Validation : Please provide a content
- minLength : 20
- slug
- type : String
- createdAt
- type : Date
- default : Date.now
- likeCount
- type : Number
- default : 0
- min : 0
- likes
- type : Array(ObjectId)
- ref : "User"
- user
- type : ObjectId
- ref : "User"
- answerCount
- type : Number
- default : 0
- answers
- type : Array(ObjectId)
- ref : Answer
- content
- type : String
- required : true
- Validation : Please provide a content
- minLength : 20
- createdAt
- type : Date
- default : Date.now
- likeCount
- type : Number
- default : 0
- min : 0
- likes
- type : Array(ObjectId)
- ref : User
- user
- type : ObjectId
- ref : User
- required : true
- question
- type : ObjectId
- ref : Question
- required : true
- Middlewares That Protect Routes From Unauthorized Access
- getAccessToRoute
- getAdminAccess
- getQuestionOwnerAccess
- getAnswerOwnerAccess
- Middlewares That Check Entities Exist With Given Ids
- checkQuestionAndAnswerExist
- checkQuestionExist
- checkUserExist
- Middleware That Captures All Errors
- errorHandler
- Middleware That Provides Advance Query Functionalities
- answerQueryMiddleware
- questionQueryMiddleware
- userQueryMiddleware
- Middleware That Provides Security to Rest Api
- limitAccess
- hpp
- cors
- helmet
- mongoSanitize
- connectDatabase
- MongoDb Connection
- customError
- Customized Error Class
- errorWrapper
- Function that catches asynchronous errors
- photoUpload
- Helper Function That Customized Upload Process with Multer Package
- sendEmail
- Helper Function That Customized Mail Process with NodeMailer Package
Environment Variables and Constants Can Be Set in ./config/env/config.env.
Distributed under the MIT License. See LICENSE
for more information.
Ahmet Çetinkaya - @ahmetctnky_txt - ahmetcetinkaya7@outlook.com