Name: Node/React Practicum Back-End
Description: This is the back-end server for a practicum project using Node.js and Express to create a RESTful API
The server communicates with a MongoDB database and provides endpoints for the front-end React application
- Node.js: JavaScript runtime environment
- Express: Node.js web application framework for building RESTful APIs
- MongoDB: NoSQL database used for storing data
- Mongoose: Object Data Modeling (ODM) library for MongoDB and Node.js
- Nodemon: Tool for automatically restarting the server during development
- Dotenv: Module to load environment variables from a .env file
- JWT: JSON Web Tokens for authentication
- Bcrypt: Library for hashing passwords
- Cors: Middleware for enabling Cross-Origin Resource Sharing
- Morgan: HTTP request logger middleware
- Body-Parser: Middleware for parsing incoming request bodies
- Swagger-ui-express: Module to provide with API's documentation via server route
Utilizing features of the Swagger-ui-express, API documentation can be accessed through /api/v1/docs
route,
eliminating the need to surf through the /controllers
and /routes
, providing necessary data in a convenient form
Also keep in mind that Render allocates minimum of its CPU time, so waiting for the
/api/v1/docs
response may take awhile
DB models are located within /models
directory:
./models/Bookmarks.js
./models/Event.js
./models/User.js
Inside these files schemas can be found
Each schema maps to a MongoDB collection and defines the shape of the documents within that collection
For example, several properties from EventSchema
:
title: {
type: String,
unique: true,
required: true
},
category: {
type: String,
enum: ['food', 'animal', 'environment', 'health', 'social'],
default:'social',
required: true
},
coordinates: {
type: [Number],
required: true,
default: undefined,
}
Each one is named and has several attributes:
type
- defines DataType, accepts different values like String, Number, Date, Array, etc
unique
- property must be unique among other documents in the collection, otherwise duplicate error is casted
required
- property is integral, document can't exist without it, otherwise considered optional
enum
- gives a list of predefined values of a certain DataType
default
- sets the starting value of the property
Some variables need to be accessible globally, while their values are static and may contain sensitive information
Dotenv is a zero-dependency module that loads environment variables from a .env
file into process.env
By default, for security purposes, .env
included into .gitignore
file, to ensure if won't be pushed to repository and become publically accessible
However, the inner structure of the file can be shown, with no sensitive data included:
MONGO_URI=mongodb+srv://<username>:<password>@<cluster-url>/practicum
JWT_SECRET=<jwt_secret>
JWT_EXPIRES=1d
PORT=8000
GOOGLE_API_KEY=<googleApiKey>
- Create a folder to contain both the front-end and back-end repos
- Clone this repository to that folder
- Run
npm install
ornpm i
to install dependencies - Pull the latest version of the
main
branch (when needed) - Run
npm run dev
to start the development server - Open http://localhost:8000/api/v1/ with your browser to test
- Your back-end server is now running. You can now run the front-end app
Note: In the below example, the group's front-end repository was named bb-practicum-team1-front
and the back-end repository was named bb-practicum-team-1-back
. Your repository will have a different name, but the rest should look the same