Want to do a Project?
As We know there are not available many resources to start the Project. So here I just thought to write a blog in which I a going to guide how to do a simple Project based on Node Js and Express Js and for the database i am going to use Mongo DB.So here we go step by step for this project.
About The Project
This is the Project of Minature StackOverflow API which is an API’s collection which can be used to make the Backend of any Fullstack Application. These are the following features in the given API’s
- Create a User
- Post a Question
- Post an Answer to the existing question
- Clap on the answer(upvote)
- Authentication and Role-based Access to the stakeholders
TECHNOLOGY STACK:
- NodeJS
- ExpressJs
- MongoDB
- EJS/scripting language(optional for UI)
- Heroku
Tasks to be Done
Day1
Start with the installation
- NodeJs ( https://nodejs.org/en/ )
- MongoDB ( https://www.mongodb.com/what-is-mongodb )
You can use the online Database ( Mongodb Atlas / m-lab)
For better UI/UX of the database you can download ( mongoDB Atlas / Robo3T ) - For editor, you can use any IDE. (my fav. — Visual Code)
- If you want the version control also then install git-bash or you can you Github desktop
Day2
- Setup Your Node-Js Project
- Make a SignUp and Login Routes for the user
Read these blogs to take help
https://medium.com/code-to-express/starting-with-nodejs-b70679e8101f
https://medium.com/code-to-express/login-and-signup-page-4a65fec162f1
Day3
So till now, a User can SignUp and log in. Now the next thing is to make the Question Schema So that a User can Post a Question and also answer to that question. Along with this User can also Upvote the answer. So please follow these steps:
- Make a Question Schema/Model(Take help from Resources)
- Make a route(private route) to post the Question.
- Make a get(public route) route to get all the Questions.
Day4
So till now, a User can Post a Question after they login into the System. So for further Development please follow these steps:
- Make a private route to post the Answer only for the existing Questions.
- Make a public route to get all the Answers and it is better to get the complete Question Model.
- It is just advice to make the array-String in Model/Schema if you want to have more than one value for the same entity. For example, we want to save many answers for only one question.
Day5
So till now, a User Can Post an Answer after they log in but anyone can read the answer and question without the login. So for the further Development Please follow these steps:
- Make a route to increase upvote array and this is a private route.
- Also, Make a separate route for the profile section where you take all the information and this is a private too.
- Again it is just advice to make the array-String in Model/Schema if you want to have more than one value for the same entity. For example, we want to save many upvotes of only one question.
Resources
For the help Go into this repo Star and fork https://github.com/HrithikMittal/Minature-Stackoverflow-APIs/
For Docs Please refer https://nodejs.org/api/http.html
and for mongoDB refer this https://www.mongodb.com/
Guidance
Person Model(User)
const PersonSchema = new Schema({
name: {
type: String,
required: true
},
email: {
type: String,
required: true
},
password: {
type: String,
required: true
},
username: {
type: String
},
profilepic: {
type: String,
default: "https://learncodeonline.in/manicon.png"
},
date: {
type: Date,
default: Date.now
}
});
Profile Model(User’s Profile)
const ProfileSchema = new Schema({
user: {
type: Schema.Types.ObjectId,
ref: "myPerson"
},
username: {
type: String,
required: true,
max: 50
},
website: {
type: String
},
country: {
type: String
},
languages: {
type: [String],
required: true
},
portfolio: {
type: String
},
workrole: [
{
role: {
type: String,
required: true
},
company: {
type: String
},
country: {
type: String
},
from: {
type: Date
},
to: {
type: Date
},
current: {
type: Boolean,
default: false
},
details: {
type: String
}
}
],
social: {
youtube: {
type: String
},
facebook: {
type: String
},
instagram: {
type: String
}
},
date: {
type: Date,
default: Date.now
}
});
Question Model
const QuestionSchema = new Schema({
user: {
type: Schema.Types.ObjectId,
ref: "myPerson"
},
textone: {
type: String,
required: true
},
texttwo: {
type: String,
required: true
},
name: {
type: String
},
upvotes: [
{
user: {
type: Schema.Types.ObjectId,
ref: "myPerson"
}
}
],
answers: [
{
user: {
type: Schema.Types.ObjectId,
ref: "myPerson"
},
text: {
type: String,
required: true
},
name: {
type: String
},
date: {
type: Date,
default: Date.now
}
}
],
date: {
type: Date,
default: Date.now
}
});
Thanks for reading and stay tuned for more blogs.