This project is a web application built with Node.js and Express, designed to manage a collection of anime and movies. It includes features for user authentication, user profiles, and admin capabilities.
To run this project locally, follow these steps:
-
Clone the repository:
git clone https://github.com/Bekzat158/web2_assig4.gitThis command will create a local copy of the repository on your machine.
-
Navigate to the project directory:
cd <project-directory> -
Install dependencies:
npm install -
Run the application:
npm start
The project structure is as follows:
.
├── app.js
├── models
│ ├── anime.js
│ ├── item.js
│ ├── movie.js
│ └── user.js
├── package.json
├── package-lock.json
├── public
│ ├── logo.jpg
│ └── styles.css
├── README.md
├── routes
│ ├── admin.js
│ ├── anime.js
│ ├── history.js
│ ├── index.js
│ ├── language.js
│ ├── login.js
│ ├── logout.js
│ ├── movie.js
│ ├── profile.js
│ └── register.js
├── translations
│ ├── en.json
│ └── ru.json
├── utils
│ ├── middleware.js
│ └── validation.js
└── views
├── admin.ejs
├── anime.ejs
├── footer.ejs
├── header.ejs
├── history.ejs
├── index.ejs
├── login.ejs
├── movie.ejs
├── profile.ejs
└── register.ejs
- app.js: Entry point of the application.
- models/: Directory containing Mongoose models for anime, movie, and user data.
- public/: Directory for static assets such as images and stylesheets.
- routes/: Directory containing route handlers for different parts of the application.
- utils/: Directory containing utility functions, including authentication and validation.
- README.md: This file, providing an overview of the project and instructions for setup.
The auth.js middleware provides functions for user authentication and authorization.
-
verifyToken: Verifies the JWT token present in the request cookies. If the token is valid, it sets
userIdandusernamein the request object and calls the next middleware. If the token is invalid or missing, it redirects the user to the login page. -
redirectToHomeIfLoggedIn: Checks if a user is already logged in by verifying the JWT token. If the token is present and valid, it redirects the user to the home page. If not, it allows the request to proceed to the next middleware.
-
ifAdmin: Verifies the JWT token and checks if the user is an admin. If the user is an admin, it allows the request to proceed to the next middleware. Otherwise, it returns a 403 Forbidden error.
The anime.js and movie.js files contain API endpoints for fetching anime and movie data from external APIs and storing them in the database.
- GET /anime: Renders the anime page with an empty animeData array.
- POST /anime: Handles the POST request to search for anime data. It queries the Jikan API (
http://api.jikan.moe/v4/anime) with the search query provided in the request body. It then saves the retrieved anime data to the database, associating it with the user's ID. Finally, it renders the anime page with the retrieved anime data.
- GET /movie: Renders the movie page with an empty movieData array.
- POST /movie: Handles the POST request to search for movie data. It queries The Movie Database API (
https://api.themoviedb.org/3/search/movie) with the search query provided in the request body. It then saves the retrieved movie data to the database, associating it with the user's ID. Finally, it renders the movie page with the retrieved movie data.
These APIs utilize JWT tokens for authentication and authorization, ensuring that only authenticated users can access them.
To use the anime and movie APIs:
- Make sure the server is running (
npm start). - Send a POST request to
/animewith a search query in the request body to search for anime data. - Send a POST request to
/moviewith a search query in the request body to search for movie data.
The APIs will handle the requests, retrieve data from the external APIs, store them in the database, and render the corresponding pages with the retrieved data.
- User authentication with JWT tokens.
- Role-based access control (admin and regular user).
- CRUD operations for anime and movies.
- User profile management.
- Express: Web application framework for Node.js.
- Mongoose: MongoDB object modeling tool.
- jsonwebtoken: JSON Web Token implementation for Node.js.
- bcryptjs: Library for hashing passwords.
- dotenv: Loads environment variables from a
.envfile. - body-parser: Middleware for parsing incoming request bodies.
- cookie-parser: Middleware for parsing cookies in Express.
- ejs: Embedded JavaScript templates for rendering views.
- axios: Promise-based HTTP client for making requests.
Contributions are welcome! Feel free to open issues or pull requests for any improvements or bug fixes.
This project is licensed under the MIT License - see the LICENSE file for details.