repository to offer a practical example and starting point for those diving into backend development with Nodejs and Express
MIT License
Ex-Server is a Node.js and TypeScript server application using Express.js, PostgreSQL with Prisma as an ORM, and Jest for testing. The server includes features such as user authentication, input validation, email verification, and rate limiting.
Clone the repository:
git clone https://github.com/yassersaidi/ex-server.git
cd ex-server
Install dependencies:
Copy code
npm install
Create a .env file in the root directory of the project and add the required environment variables:(check .env.example)
Set up your PostgreSQL database and run Prisma migrations:
npx prisma migrate dev
npx prisma generate
Start the development server:
npm run dev
The server will be running at http://localhost:3001.
Method | Endpoint | Request Body | Response |
---|---|---|---|
POST | /auth/login | { "email": "[email protected]", "password": "password123" } |
{ "accessToken": "token", "user": { "id": "userId", "email": "[email protected]", "username": "username" } } |
POST | /auth/register | { "email": "[email protected]", "username": "username", "password": "password123" } |
{ "userId": "newUserId" } |
POST | /auth/verify-email | { "email": "[email protected]" } |
{ "message": "Verification code sent!" } |
POST | /auth/verify-code | { "email": "[email protected]", "code": "123456" } |
{ "message": "User successfully verified" } |
POST | /auth/forgot-password | { "email": "[email protected]" } |
{ "message": "Password reset code sent!" } |
POST | /auth/reset-password | { "email": "[email protected]", "code": "123456", "password": "newPassword123" } |
{ "message": "Password successfully reset" } |
POST | /auth/rt | (No request body; token is retrieved from cookies) | { "accessToken": "newAccessToken" } |
POST | /auth/logout | (No request body; token is retrieved from cookies) | { "message": "Logged out successfully." } |
Method | Endpoint | Request Body | Response |
---|---|---|---|
GET | /user/all | { "userId": "userId" } |
[ { "id": "userId", "email": "[email protected]", "username": "username", "createdAt": "2024-01-01T00:00:00Z", "verified": true, "profilePicture": "url" }, ... ] |
GET | /user/me | { "userId": "userId" } |
{ "id": "userId", "email": "[email protected]", "username": "username", "createdAt": "2024-01-01T00:00:00Z", "verified": true, "profilePicture": "url" } |
GET | /user/get/:username | { "userId": "userId" } |
{ "id": "userId", "email": "[email protected]", "username": "username", "createdAt": "2024-01-01T00:00:00Z", "verified": true, "profilePicture": "url" } |
PUT | /user/username | { "userId": "userId", "username": "newUsername" } |
{ "id": "userId", "email": "[email protected]", "username": "newUsername", "createdAt": "2024-01-01T00:00:00Z", "verified": true, "profilePicture": "url" } |
PUT | /user/update-image |
{ "userId": "userId" } (Image uploaded via form-data) |
{ "message": "Image updated successfully", "updatedUser": { "id": "userId", "email": "[email protected]", "username": "username", "createdAt": "2024-01-01T00:00:00Z", "verified": true, "profilePicture": "url" } } |
GET | /user/search | { "query": "searchTerm" } |
[ { "id": "userId", "email": "[email protected]", "username": "username", "createdAt": "2024-01-01T00:00:00Z", "profilePicture": "url", "verified": true }, ... ] |
DELETE | /user/ | { "userId": "userId" } |
{ "count": 1 } |
/auth/rt
and /auth/logout
), the table indicates that the token is retrieved from cookies.To run tests, use the following command:
npm run test
To set up and run your application with Docker, just run this commande:
docker-compose up --build