Thank you very much for your interest in my project. It's the last proyect, a complete full stack project with Geekshubs Academy of the Full Stack Development Bootcamp 🚀.
Full Description 📝
-
Users can register for the application, log in and access it.
They can search for a destination, choose from different types of cars and make the trip.
A driver responds to the request and picks up the passenger.
Once the trip is completed, they can rate the driver.
Users must complete their profile and choose the payment method.
They can also access their trip history.
View endopints
- Register Users
- Login
- Register Drivers
- Login
- Get All Users with pagination
- Pagination example
- Get User Profile
- Update User Profile
- Delete User by Id
- Delete one User or more by Id arrays (superAdmin)
- Get Users with search by query
- Create Trip
- Get all Trips (Super_admin)
- Get my all Trips (TOKEN)
- Get trips by Id (TOKEN)
- Update Trip (TOKEN)
- Update Trip by Id (TOKEN)
- Delete Trip by Id (TOKEN)
- Delete more than one Trips by Id array (superAdmin)
- Get All Drivers with pagination
- Pagination Example
- Get Driver Profile
- Update Driver Profile
- Delete Driver by Id
- Delete one Driver or more by Id arrays (superAdmin)
- Authentication
POST localhost:5500/auth/register
POST https://localhost:5500/auth/login
POST localhost:5500/auth/drivers/register
POST localhost:5500/auth/drivers/login
- Users
GET http://localhost:5500/users
"url_example_of_pagination":"http://localhost:5500/users?page=2",
"url_example_of_pagination1":"http://localhost:5500/users?limit=20",
"url_example_of_pagination2":"http://localhost:5500/users?page=3&limit=15"
GET http://localhost:5500/users/profile
PUT http://localhost:5500/users/profile
{ "userName": "Jhon Smith", "phone": "+3461371436", "address": "carrer xativa 20", "payment": "credit" }
DELETE http://localhost:5500/users/:id
DELETE http://localhost:5500/users
{ "usersId" : [4,5,7,5,78,2] }
{ "usersId" : [12] }
GET http://localhost:5500/users/search?search=email
GET http://localhost:5500/users/search?search=name
POST http://localhost:5500/trips/
{ "startLocation": "[39.5102451,-0.4115579]", "destination": "Valencia, Spain", "driverId": 3 }
GET http://localhost:5500/trips/all
PUT http://localhost:5500/trips/:id
GET http://localhost:5500/trips
PUT http://localhost:5500/trips/
{ "tripDate": "2024-05-01 19:00:00", "trip_id": 23, "driverId": 4, "startLocation": "Godella", "destination":"Palau de Congresos", "carId":2 }
PUT http://localhost:5500/trips/:id
DELETE http://localhost:5500/trips/:id
DELETE http://localhost:5500/trips
{ "tripsId" : [4,2,5,6,8,10] }
{ "tripsId" : [6] }
GET http://localhost:5500/drivers
url_example_of_pagination":"http://localhost:5500/drivers?page=2
"url_example_of_pagination1":"http://localhost:5500/drivers?limit=20",
"url_example_of_pagination2":"http://localhost:5500/drivers?page=3&limit=15"
GET http://localhost:5500/drivers/profile
PUT http://localhost:5500/drivers/profile
DELETE http://localhost:5500/drivers/:id
DELETE http://localhost:5500/drivers
{ "driversId" : [4,5,7,5,78,2] }
{ "driversId" : [12] }
All non-public endpoints with corresponding middlewares
- We can find here the collection of all endpoints in Thunder Client:
You have to open Thunder Client
Go to collections
Import this file:
./HTTP/thunder-collection_TAXI APP.json
- Install docker and run this command to get a container
- Example
- Run follow command to create "package-lock.json" install node_modules
- Create .gitignore in root and add "./node_modules" , ".env" and ".dist" to avoid upload to github repository
- Create the 'tsconfig.json' file
- Install types/express & node
- Install dependencies to compile TS (nodemon)
- Create ".env" and ".env.example" file The .env file has the key & value credentials to access to the data base. It should not be visible, for this reason we add it to .gitingnore. The ".env.example" files have the same structure to build your ".env" file on your local
- Put the follow keys in .env file.
- Install 'dotenv' to added th depencencies and will grab data from the .env file
- DOTENV - Create the folder "src" with "server.ts" file inside. This the code to connect to the server.
- We create app.ts file
- Think and rethink the database, avoid redundancy between keys and related tables. Identify primary keys (PK) and foreign keys (FK). Choose the type of value, if it is 'NULL' (not required) or can be 'UNIQUE' fields.
- Open Mysql Workbench and set up new dataBase connection
-
$ npm run dev
-
Creating MIGRATIONS [Data Definition Language (DDL): with typeorm]:
$ npm run run-migrations
./src/database/migrations
- It's possible that's need populate with one basic table before continue with the migrations go to the point 11.Run Project
-
Adding them to
DataSource.migrations
in thedb.ts
file:Role, User, Driver, Trip, Car, Brand
-
Creating MODELS (entities) [Data Manipulation Language (DML)]
-
Adding them to
DataSource.entities
in thedb.ts
file:Roles, Users, Drivers, Trips, Cars, Brands
- We create controllers (in a folder on the same level with
package.json
): >auth, users, drivers, trips
- We create routes (in
app.ts
) for CRUD (create, read, update and delete) database records for users ,drivers, trips. - Additionally we need to control access to our data. We will use 'middleware' functions.
- Auth
(authorization system based on TOKENs) will block anything that should not be seen by the general public. In our case, it only does not affect
register,
loginand
getServices` (since those are the endpoints accessible without logging in). - The
auth()
function verifies an encrypted TOKEN created automatically at login. With an active token we have access to other data. - We also want to grant special administrative access. With another middleware, the
isSuperAdmin()
function, we control PERMISSIONS. - The 'superadmin' role would be able to reach all data, while Users would have a more limited reach. More levels can be implemented
-
For the TOKEN to work, we create a new file
./types/index.d.ts
with the following lines: We must add driverId, because in this project we have auth users and auth drivers, and we need both to use tokenData.export type TokenData = { driverId: number userId: number roleName: string } declare global { namespace Express { export interface Request { tokenData: TokenData } } }
- In order to check out this project, you'll need to populate the database. We create a seeders.ts file, we use the npm faker library to create fake users, cars and drivers.
- Clone this repository
- Run in terminal
- Conect repository with database
- Run migrations:
- Run seeders:
- Start server:
- - super_admin & random user - Credentials
1. INSTALL DOCKER
$ docker run --name mysql-exampleName -p 3309:3306 -e MYSQL_ROOT_PASSWORD=1234 -d mysql
$ mysql -h localhost -P 3306 -u root -p you will need -h (host), -P (port), -u(username) and -p (password)
2. INSTALL EXPRESS
$ npm init
$ npm install express --save
$ npm install typescript -D
$ npx tsc --init
$ npm install @types/express @types/node -D
$ npm install ts-node nodemon -D
PORT =4XXX
#conexion a bd
DB_USER=root
DB_PASSWORD=XXXX
DB_PORT=33XX
DB_HOST=localhost
DB_DATABASE=dataBaseName
# JWT
JWT_SECRET=xxxxxx
$ npm install dotenv
import "dotenv/config"
import { app } from "./app"
import { AppDataSource } from "./database/db"
const PORT = process.env.PORT || 5500
const startServer = () => {
AppDataSource.initialize()
.then(() => {
console.log("database connected")
app.listen(PORT, () => {
console.log(`Server is running on port: ${PORT}`)
})
})
.catch((error: any) => {
console.log("error")
})
}
startServer()
// links to .env file
import express, { Application } from "express"
import "dotenv/config"
import { authRouter } from "./routes/authRoutes"
// export app function export
const app: Application = express()
// parses response to .json
app.use(express.json())
// testing request
app.get("/healthy", (req, res) => {
res.status(200).json({
success: true,
message: "Server is healthy",
})
})
// auth Users & Drivers
app.use("/", authRouter) //<-- Route Example
3. CREATE MYSQL
4. MIGRATIONS & MODELS
5. CONTROLLERS
6. ROUTES
7. MIDDLEWARE: auth( )
8. MIDDLEWARE: isSuperAdmin()
9. TOKENDATA
10. SEEDERS
$ npm run seed
11. RUN PROJECT
$ npm install
$ npm run run-migrations
$ npm run seeders
$ npm run dev
{
"first_name": "Super",
"last_name": "Super"
"email": "[email protected]"
"password": 123456,
"role_id": 3
}
{
"first_name": "user",
"last_name": "user"
"email": "[email protected]"
"password": 123456,
"role_id": 1
}
The next steps is deploy the full proyect