An open API service indexing awesome lists of open source software.

https://github.com/mohosin2126/automotive-server

This MERN Stack code establishes an Express server connected to MongoDB. It handles brand-related data, products, and user carts, providing CRUD operations and user authentication functionalities.
https://github.com/mohosin2126/automotive-server

Last synced: 3 months ago
JSON representation

This MERN Stack code establishes an Express server connected to MongoDB. It handles brand-related data, products, and user carts, providing CRUD operations and user authentication functionalities.

Awesome Lists containing this project

README

        

# automotive-server

- Importing Dependencies:

The code starts by importing necessary modules: MongoClient, ServerApiVersion, and ObjectId from the 'mongodb' package, and 'express' for creating the web server.
The 'cors' module is imported to handle Cross-Origin Resource Sharing.
The 'dotenv' module is imported to handle environment variables.
- Setting Up Express and Middleware:

An instance of the Express application is created.
CORS and JSON middleware are applied to handle cross-origin requests and parse JSON data.
- MongoDB Connection:

The MongoDB connection URI is constructed using environment variables.
A new instance of the MongoDB client is created with specific server API version settings.
- Routes and Collections:

Several collections are defined, representing different entities in the MongoDB database ('brandsCollection', 'allBrandsCollection', 'brandUpdateCollection', and 'mycartCollection').
- Various routes are defined using Express to handle different operations:
'/brands': Retrieves all brands.
'/allBrands': Retrieves all brands.
'/addProduct': Adds a new product to the 'brandUpdateCollection'.
'/mycart': Handles adding, retrieving, and deleting products from the user's cart.
'/allBrands/:id': Retrieves a specific brand by ID.
'/allBrands/:id': Updates a specific brand by ID.
- Server Initialization:

The server is configured to listen on a specified port, either from the environment variable or the default port 5001.
A simple root route is defined to respond with 'Hello World!' when accessed.
- MongoDB Connection Check:

Inside the 'run' function, there's a commented-out line to ping the MongoDB deployment to check the connection status. This is currently not active.
- Run Function and Error Handling:

The 'run' function is an asynchronous function where the MongoDB client connects to the database.
Collections are initialized, and routes are set up.
The server is started, and any errors during this process are caught and logged.
- Closing the MongoDB Connection (Currently Commented Out):

There's a commented-out line to close the MongoDB connection, which is typically done when the application shuts down. Currently, it's not active.
- Server Start Message:

A message is logged to the console indicating that the server is running and connected to MongoDB.
- Root Route ('/'):

A simple route is defined to respond with 'Hello World!' when accessed. This is just for testing if the server is running.