This repository contains the source code for a blog project developed using Express.js and Prisma.
- Node.js
- MySQL
-
Clone the repository:
git clone https://github.com/Haitam-Elgharras/projet-web.git
-
Change into the project directory:
cd projet-web
-
Install the dependencies:
npm install
-
Start the server:
npm start
-
Access the application in your web browser:
The project follows a typical structure generated by the Express generator. It separates routes and models to keep the code organized and maintainable. The models directory contains functions that are used in the routes to make database queries and perform operations on the data.
The project utilizes an authentication middleware to verify user credentials before performing certain operations. This ensures that only authenticated users can access protected routes and perform authorized actions.
The project uses Prisma as the ORM (Object-Relational Mapping) tool to interact with the MySQL database. Prisma simplifies database operations by providing a type-safe API for querying and manipulating data. It also handles database migrations for seamless schema updates.
To set up the database:
-
Install Prisma as a dev dependency:
npm install prisma -D
-
Initialize Prisma:
npx prisma init
-
Add the database URL to the
.env
file. -
Define the database schema in the
schema.prisma
file. -
Apply migrations to create the initial database structure:
npx prisma migrate dev --name init
Prisma Studio provides a visual interface to explore and interact with the database. It allows you to view tables, records, and relationships, making it easier to debug and test your database.
To launch Prisma Studio:
npx prisma studio
The frontend of the application includes features such as displaying articles, a navigation bar, adding comments, adding new articles, and filtering articles by category.
To ensure a seamless user experience, the application implements token-based authentication using JSON Web Tokens (JWT). When a user registers or logs in, a token is generated on the server and sent back to the frontend. This token is stored in the local storage and sent with subsequent requests to authenticate the user. The session remains active even if the user refreshes the page.
When the application loads, the window.onload
event is triggered. It checks if a valid token exists in the local storage. If so, it automatically authenticates the user, allowing them to access protected routes without requiring manual login.
To log out, the application removes the token from the local storage, effectively ending the session.
To contribute to this project, follow these steps:
- Fork the repository on GitHub.
- Create a new branch.
- Make your modifications and commit your changes.
- Push your changes to your forked repository.
- Submit a pull request.
This project is maintained by Haitam Elgharras. Feel free to reach out if you have any questions or feedback.
This project is licensed under the MIT License.