REST server with Node.js for maintenance practice, showcasing robust backend development using Node.js, Express, Mongoose, and Express Validation.
-
Clone the repository:
git clone https://github.com/your-username/rest-server-maintenance.git cd rest-server-maintenance
-
Install dependencies:
npm install
-
Create a
.env
file in the project root based on theexample.env
template:PORT=3000 MONGODB_CNN=mongodb://your_mongodb_connection_string JWT_SECRET=your_jwt_secret_or_private_key GOOGLE_CLIENT_ID=your_google_client_id GOOGLE_CLIENT_SECRET=your_google_client_secret
npm run dev
: Start the development server using Nodemon andindex.js
.npm start
: Start the server using Node.js.
-
Sign In (Login):
POST /api/auth/login
- Request Body:
{ "email": "string", "password": "string" }
- Response: Returns a JWT (JSON Web Token) for authentication.
- Request Body:
-
Sign In with Google:
POST /api/auth/google
- Redirects to Google OAuth for authentication.
-
Sign Up (Register User):
POST /api/auth/signup
- Request Body:
{ "name": "string", "email": "string", "password": "string" }
- Request Body:
-
Get All Users:
GET /api/user
- Requires JWT in
x-token
header for authentication.
- Requires JWT in
-
Get User by ID:
GET /api/user/:id
- The
id
should be a valid user ID. - Requires JWT in
x-token
header for authentication.
- The
-
Create a New User:
POST /api/user
- Request Body:
{ "name": "string", "email": "string", "password": "string", "role": "string (optional)", "active": "boolean" }
- Requires JWT in
x-token
header for authentication.
- Request Body:
-
Update User by ID:
PUT /api/user/:id
- The
id
should be a valid user ID. - Request Body:
{ "name": "string", "email": "string", "password": "string", "role": "string (optional)", "active": "boolean" }
- Requires JWT in
x-token
header for authentication.
- The
-
Delete User by ID:
DELETE /api/user/:id
- The
id
should be a valid user ID. - Requires JWT in
x-token
header for authentication.
- The
-
Get Categories with Pagination:
GET /api/categories?limit=2&page=1
- Requires JWT in
x-token
header for authentication.
- Requires JWT in
-
Get Category by ID:
GET /api/categories?id=string
- The
id
should be a valid MongoDB ID. - Requires JWT in
x-token
header for authentication.
- The
-
Create a New Category:
POST /api/categories
- Request Body:
{ "name": "string", "status": "boolean" }
- Requires JWT in
x-token
header for authentication.
- Request Body:
-
Update a Category:
PUT /api/categories?id=string
- The
id
should be a valid MongoDB ID. - Request Body:
{ "name": "string", "status": "boolean" }
- Requires JWT in
x-token
header for authentication.
- The
-
Delete a Category:
DELETE /api/categories?id=string
- The
id
should be a valid MongoDB ID. - Requires JWT in
x-token
header for authentication.
- The
-
Get Products with Pagination:
GET /api/products?limit=10&page=1
- Requires JWT in
x-token
header for authentication.
- Requires JWT in
-
Create a New Product:
POST /api/products
- Request Body:
{ "name": "string", "status": "boolean", "price": "number", "category": "string (MongoDB ID)", "description": "string", "available": "boolean" }
- Requires JWT in
x-token
header for authentication.
- Request Body:
-
Update a Product:
PUT /api/products?id=string
- The
id
should be a valid MongoDB ID. - Request Body similar to creation of product.
- Requires JWT in
x-token
header for authentication.
- The
-
Delete a Product:
DELETE /api/products?id=string
- The
id
should be a valid MongoDB ID. - Requires JWT in
x-token
header for authentication.
- The
- Upload File:
POST /api/upload
- Request Body: form-data with
file
property. - Requires JWT in
x-token
header for authentication.
- Request Body: form-data with
To access protected endpoints that require authentication, include the JWT (JSON Web Token) obtained during login in the request headers.
Include JWT in Request Headers:
x-token: <your_jwt_here>
Replace <your_jwt_here>
with the JWT token obtained from the login response.
Example Usage (using cURL):
# Replace <your_jwt_here> with your actual JWT token
curl -X GET \
'https://your-base-url/api/categories' \
-H 'x-token: <your_jwt_here>'
To log in using Google OAuth, visit the following URL:
https://your-base-url/api/auth/google
This endpoint will redirect you to Google's authentication page to sign in with your Google account.
- Node.js
- Express
- Mongoose
- Express Validation
Feel free to contribute by opening issues or submitting pull requests. Your feedback is highly appreciated!
This project is licensed under the MIT License.a