CommunityBE is a backend setup project that serves as the backend infrastructure for a community-based application. It provides API endpoints for managing users and posts within the community platform.
- Node.js installed on your machine (Node.js official website)
- MongoDB database setup and running
- Clone the repository from GitHub.
- Navigate to the project directory in your terminal.
- Run
npm install
to install dependencies.
This project utilizes environment variables for configuration. Create a .env
file in the root directory of the project and define the following variables:
You can copy and paste this Markdown directly into your documentation. It will render as follows:
This project utilizes environment variables for configuration. Create a .env
file in the root directory of the project and define the following variables:
PORT=8000
MONGODB_URI=mongodb://localhost:27017/community_db
To start the server, use one of the following npm scripts:
npm start
: Starts the server using Node.js.npm run server
: Starts the server using Nodemon for automatic restarts during development.
- GET /api/users: Get a list of all users.
- GET /api/users/:id: Get details of a specific user by ID.
- GET /api/posts: Get a list of all posts.
The project follows the following directory structure:
communitybe/
│
├── backend/
│ ├── config/
│ │ └── db.js
│ ├── middleware/
│ │ └── errorMiddleware.js
│ ├── routes/
│ │ ├── postRoutes.js
│ │ └── userRoutes.js
│ ├── controllers/
│ │ ├── postController.js
│ │ └── userController.js
│ └── server.js
│
├── models/
│ ├── Post.js
│ └── User.js
│
├── .env
├── package.json
└── README.md
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const userSchema = new Schema(
{
name: {
type: String,
required: true,
},
email: {
type: String,
required: true,
unique: true,
},
password: {
type: String,
required: true,
},
},
{ timestamps: true }
);
const User = mongoose.model('User', userSchema);
module.exports = User;
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const postSchema = new Schema(
{
title: {
type: String,
required: true,
},
content: {
type: String,
required: true,
},
},
{ timestamps: true }
);
const Post = mongoose.model('Post', postSchema);
module.exports = Post;
This Markdown representation will display the Post Schema code snippet with proper syntax highlighting and formatting:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const postSchema = new Schema(
{
title: {
type: String,
required: true,
},
content: {
type: String,
required: true,
},
},
{ timestamps: true }
);
const Post = mongoose.model('Post', postSchema);
module.exports = Post;
-
GET /api/users
- Description: Retrieves a list of all users.
- Controller:
userController.getUsers
-
GET /api/users/:id
- Description: Retrieves details of a specific user by ID.
- Controller:
userController.getUserById
- GET /api/posts
- Description: Retrieves a list of all posts.
- Controller:
postController.getPosts
- cors: ^2.8.5
- dotenv: ^16.4.5
- express: ^4.19.2
- express-async-handler: ^1.2.0
- mongodb: ^6.6.1
- mongoose: ^8.3.4
- nodemon: ^3.1.0
- Katiuska
This project is licensed under the MIT License.