A Node.js and Express backend for an AI-powered wallpaper generation platform, providing APIs for image processing, user management, and storage with MongoDB. Integrates with AI models to generate and serve custom wallpapers, ensuring scalability and secure data handling.
This is the backend for the AI Wallpapers website, which handles user authentication, wallpaper management, and other related operations. The backend is built with Node.js, Express.js, and MongoDB.
The project has the following folder structure:
/ai-wallpapers-backend
│
├── /controllers
│ └── imageController.js # Contains logic for handling image-related requests (CRUD operations)
│ └── userController.js # Contains logic for handling user registration, login, and profile updates
│
├── /models
│ └── image.js # Mongoose schema for Image metadata, including image details, tags, and more
│ └── user.js # Mongoose schema for User, including name, email, password, etc.
│
├── /routes
│ └── imageRoutes.js # API routes for handling image operations like create, get, update, delete, etc.
│ └── userRoutes.js # API routes for handling user authentication and profile management
│
├── /middleware
│ └── authMiddleware.js # Middleware for protecting routes and validating JWT tokens
│
├── /utils
│ └── pagination.js # Utility functions for paginating the list of images
│
├── server.js # Main entry point for the server, where Express is configured and API routes are connected
├── package.json # Project dependencies and metadata
├── .env # Environment variables (e.g., database URL, JWT secret)
Follow the instructions below to set up and run the backend locally.
Clone this repository:
git clone <repository-url>
cd ai-wallpapers-backend
Install the required dependencies:
npm install
.env
file in the root directory..env
file:MONGO_URI=<your-mongo-db-uri>
JWT_SECRET=<your-jwt-secret-key>
To start the backend server, run the following command:
npm start
The server will run on http://localhost:3000
.
The following are the main API routes for managing wallpapers and user authentication:
POST /images
: Create a new image entry.
imageName
, imageUrl
, description
, tags
, size
, format
, category
, resolution
GET /images
: Get all images.
GET /images/:id
: Get a single image by its ID.
id
(image ID)PUT /images/:id
: Update an image by its ID.
id
(image ID)imageName
, imageUrl
, description
, tags
, size
, format
, category
, resolution
DELETE /images/:id
: Delete an image by its ID.
id
(image ID)GET /images/category/:category
: Fetch images by category.
category
(category name)GET /images/search
: Search images by name or tags.
query
(search term)GET /images/paginate
: Paginate images.
page
, limit
(default values are page=1, limit=10)POST /register
: Register a new user.
name
, email
, password
POST /login
: Log in an existing user.
email
, password
GET /profile
: Get the authenticated user’s profile.
PUT /profile
: Update the authenticated user’s profile.
name
, email
DELETE /profile
: Delete the authenticated user’s account.
/controllers
Contains the logic for handling requests and interacting with the models.
imageController.js
: Handles the image-related logic for creating, updating, deleting, and fetching images.userController.js
: Handles user registration, login, profile management, and account deletion./models
Contains Mongoose schemas.
image.js
: Defines the schema for image metadata.user.js
: Defines the schema for user, including name, email, password, etc./routes
Contains the API routes.
imageRoutes.js
: Defines the routes for image-related operations.userRoutes.js
: Defines the routes for user authentication and profile management./middleware
Contains middleware functions for protecting routes.
authMiddleware.js
: Middleware that checks for a valid JWT token and attaches the authenticated user to the request object./utils
Contains utility functions.
pagination.js
: Utility function for paginating the list of images./config
Contains the database configuration.
db.js
: Configures the connection to the MongoDB database.server.js
The main entry point of the application, where the Express server is set up, middleware is added, and routes are connected.
POST http://localhost:3000/register
Content-Type: application/json
{
"name": "John Doe",
"email": "[email protected]",
"password": "password123"
}
POST http://localhost:3000/login
Content-Type: application/json
{
"email": "[email protected]",
"password": "password123"
}
GET http://localhost:3000/profile
Authorization: Bearer <your-jwt-token>
PUT http://localhost:3000/profile
Authorization: Bearer <your-jwt-token>
Content-Type: application/json
{
"name": "Johnathan Doe",
"email": "[email protected]"
}
DELETE http://localhost:3000/profile
Authorization: Bearer <your-jwt-token>