MERN Sustainability Project Backend
This project is the backend for the MERN Sustainability Project, a Node.js application using Express, MongoDB, and Mongoose to manage and interact with business data. It provides RESTful API endpoints for creating, retrieving, and managing business information.
- Setup
- Project Structure
- Environment Variables
- API Endpoints
- Models
- Error Handling
- Running the Server
- License
To get the project up and running locally, follow these steps:
-
Install Dependencies: Run the following command to install all necessary dependencies: npm install
-
Navigate to the src Directory: Move into the src directory where the main server code resides: cd src
-
Create a .env File: In the src directory, create a .env file with the following environment variables: PORT: The port number where your server will run. MONGO_URI: The connection string for your MongoDB database.
-
Start the Server: Run the following command to start the server:
node server.js
The server will be running on the port specified in the .env file, or 5000 if no port is specified.
Project Structure The project is organized as follows: .
├── src │ └── server.js # Main server file │ └── models │ │ └── Business.js # Mongoose model for the Business entity └── .env # Environment variables file (not included in the repository)
Environment Variables The project relies on the following environment variables:
PORT: The port number on which the server will listen.
MONGO_URI: The MongoDB connection string used by Mongoose to connect to the database.
These variables should be defined in a .env file located in the src directory.
- Description: Returns a simple welcome message.
- Response:
- Status:
200 OK
- Body: "MERN Sustainability Project Backend"
- Status:
- Description: Placeholder endpoint. Currently returns an empty response.
- Response:
- Status:
200 OK
- Body: "" (empty string)
- Status:
- Description: Creates a new business entry.
- Request Body:
- JSON object with the following fields:
- name (string, required)
- description (string, required)
- contactEmail (string, required)
- contactPhone (string, required)
- address (string, required)
- website (string, optional)
- category (string, required)
- products (array of strings, optional)
- Response:
- Status: 201 Created if successful
- Status: 500 Internal Server Error if an error occurs
- Description: Retrieves a list of all businesses.
- Response:
- Status:
200 OK
- Body: JSON array of business objects
- Status:
- Description: Retrieves a single business by its ID.
- Request Parameters:
- id (string): The ID of the business to retrieve
- Response:
- Status: 200 OK if the business is found
- Status: 404 Not Found if the business is not found
- Status: 400 Bad Request if the provided ID is invalid
The Business model is defined in models/Business.js using Mongoose. It represents the schema for a business entity with the following fields:
name (string, required): The name of the business.
description (string, required): A description of the business.
contactEmail (string, required): The contact email for the business.
contactPhone (string, required): The contact phone number for the business.
address (string, required): The address of the business.
website (string, optional): The website of the business.
category (string, required): The category of the business.
products (array of strings, optional): A list of products offered by the business.