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 folllowing features in the given API's
- Create a User
- Post a Question
- Post a Answer to the existing question
- Clap on the answer(upvote)
- Auhtentication and Role based Access to the StackHolders
- NodeJS
- ExpressJs
- MongoDB
- EJS/scripting language(optional for UI)
- Heroku
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 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
- Setup Your Node-Js Project
- Make a SignUp and Login Routes for the user
https://medium.com/code-to-express/starting-with-nodejs-b70679e8101f
https://medium.com/code-to-express/login-and-signup-page-4a65fec162f1
So till now a User can SignUp and Login. Now the Next thing to 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.
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 same entity. For example we want to save many answers of only one question.
So till now a User Can Post a Answer after they login but anyone can read the anser and question without the login. So for the further Development Please follow these Steps:
- Make a route to increase upvote array and this is an private route.
- Also Make an Seperate 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 same entity. For example we want to save many upvote of only one question.
========================================================================================================================================
To work with the api you must have to install the following:
- NodeJS - Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine.
- MongoDB Server - NoSql Database and server
- Postman - API development environment
Before doing anything you have to clone or download and unzip the project folder, open terminal and navigate to the project folder and run:
npm install
This will install all the dependencies required by the project.
To start using this API, start your local database server, open terminal and navigate to the project folder and run:
npm run start
If an error occur, check your database server or check if you have installed the prerequisites correctly.
If there was no error, open Postman and create and send a new get request to:
http://localhost:3000/
Expected Output:
{
message: "Welcome!"
}
I used express-session to manage sessions to authenticate. We have
isUserLoggedIn, isUserLoggedOut middleware function which checks if the user is
authenticated or not. The session token is stored in the database using
connect-mongo package and is deleted when the user logout
async function isUserLoggedIn (req, res, next) {
try {
if (!(req.session && req.session.user)) {
return res.status(401).send({
error: "Unauthorized Access!"
});
}else {
const user = await User.findOne({ _id : req.session.user._id })
if(user) {
next();
} else {
req.session.user = null;
return res.status(401).send({
error: "Unauthorized Access!"
});
}
}
} catch(e) {
res.status(400).send({
error: e
})
}
}
// Function to check whether the user is logged out
function isUserLoggedOut (req, res, next) {
if (req.session && req.session.user) {
return res.status(200).send({
message: "User already Logged In!"
});
}
next();
}
module.exports = {
isUserLoggedIn,
isUserLoggedOut
}
Note: some of the APIs which are mentionted above are not authenticate so please remember to add it. So it will help to proctect the private routes.
S.No. | Route | Method | Access | Description |
---|---|---|---|---|
1. | / | GET | Private | to get personal profile |
2. | / | POST | Private | for UPDATING/SAVING personnal user profile |
3. | /:username | GET | Public | for getting user profile based on USERNAME. |
4. | /find/everyone | GET | Public | for getting user profile of EVERYONE. |
5. | / | DELETE | Private | for deleting user based on ID. |
6. | /workrole | POST | Private | for adding work profile of a person. |
7. | /workrole/:w_id | DELETE | Private | for deleting a specific workrole. |
S.No. | Route | Method | Access | Description |
---|---|---|---|---|
1. | / | GET | PUBLIC | for showing all questions. |
2. | / | POST | Private | for submitting questions. |
3. | /answers/:id | POST | Private | for submitting answers to questions. |
4. | /upvote/:id | POST | Private | for upvoting. |
This api can be hosted on platform like heroku, aws, and others. MongoDB Atlas
or Matlab can be used for remote database.
For instance, the application
can be deployed on Heroku by creating and
registering an account. Following, create a new app and choose a deployment
method (terminal or github) and follow the instruction there. Remote database
can be created using Mongodb Atlas or Matlab.
For
Mongodb Atlas,
you need to just to create your account and make a new cluster and link the
cluster to your application through a URL. Following the given steps, you would
have a remote application up and running.
If you are the helping and contributing one, your efforts and suggestion are always welcomed.