A Node JS Project which is a miniature model of Stack Overflow Backend where users can post questions, answers and also upvote the answers. The users have login feature and has to be authenticated to upvote an answer. User Data is fetched from Mongo DB.
A Node JS Project where the User gets option to Login, all the user details are stored in Mongo DB. This application has features to post questions in the forum where users can answer them or even upvote them. For the answer to get upvoted the user Object ID has to be fetched from Mongo DB, which also means that the user has to be logged in/authenticated.
This project is completely deployable and can be used with any FrontEnd. It uses RESTful API and explicitly takes advantages of HTTP methodologies.
To run this project on your system you need to meet these criterias:
- Basic understanding of Node.js and JavaScript.
- A terminal app for MacOS and Linux or PowerShell for Windows.
- Node.js v8+ and a Node.js package manager installed locally.
To install Node.js and NPM, use any of the official Node.js installers provided for your operating system.
Create a project directory named Bigstack
to hold your application, and make that your working directory.
Use the npm init
command to create a package.json
file for your application.
Inside package.json
set the 'main'
to "server.js"
as that is going to be our primary end point of the application.
Instead of using the node
command to run the application, we'll use, a tool called nodemon that monitors our application and automatically restarts the server when source code changes. With node
, we'd have to restart the server manually when changes are made.
Install nodemon as a dependency of your Node.js application:
npm install nodemon
To make nodemon recognize the entry point of our application we need to map it with our main javaScript file. To do that open package.json
and add the following line, after main:
"scripts": {
"start": "node server.js"
}
To install these dependencies simply type this in the terminal:
npm install express body-parser mongoose passport bcryptjs passport-jwt validator jsonwebtoken
--save
is not required to be typed since after node v5+ release all the packages are automatically saved inside package.json
This is the primary end point of the application. This javaScript file handles the Strategies as well as routes.
It creates a running connection with Mongo DB and displays a message in the terminal on which port the web server is currently running and also if Mongo DB is successfully connected or not.
It connects our application to the RESTful API through custom routes.
In this project we have three schemas namely Profile Schema, Question Schema and User Schema.(They can be modified according to the developer as per what details they want to take from the Users).
Profile Schema -- It takes neccessary informations like username and launguges, other information are optional. The user data is fetched from Mongo DB by their Object ID :
user: {
type: Schema.Types.ObjectId,
ref: "myPerson"
}
Where myPerson
is the Mongoose Model for the User Schema.
Question Schema -- In this Schema compulsory attributes are textone, texttwo and answers.text. Again the user data is fetched from Mongo DB to only allow the logged in users to answer questions and also to upvote.
user: {
type: Schema.Types.ObjectId,
ref: "myPerson"
}
Where myPerson
is the Mongoose Model for User Schema.
User Schema --
This Schema is used to register users for the first time. It has necessary attributes like name, email, password and gender
Based on the gender it switches the default avatar picture. It adds the user to the database and creates a Mongoose Model called myPerson
This project is subject to change by the developer and is still in development
Pull Requests are welcome. Please follow these rules for the ease of understanding:
- Make sure to check for available issues before raising one
- Give me a maximum of 24-48 hours to respond
- Have proper documentation on the parts you are changing/adding
Sayan Mondal ๐ท Insta ๐ค Twitter
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
For help getting started with Node, view our online documentation.