Week 13.1 Notes
Week 13.1 Notes
Week 13.1 Notes
1 notes
Week 13.1
Building Medium
Up until now, our discussions have primarily revolved around theoretical concepts.
In this lecture, Harkirat takes a practical approach by guiding us through the hands-
on process of building a Medium like application
We'll be applying the knowledge we've gained so far, specifically focusing on
implementing the frontend using React and the backend using Cloudflare Workers —
creating a modern fullstack application.
3. zod as the validation library, type inference for the frontend types
mkdir medium
cd medium
1. POST /api/v1/signup
2. POST /api/v1/signin
3. POST /api/v1/blog
4. PUT /api/v1/blog
5. GET /api/v1/blog/:id
💡
https://hono.dev/api/routing
Solution
postgres://avnadmin:password@host/db
prisma://accelerate.prisma-data.net/?api_key=eyJhbGciOiJIUzI1
NiIsInR5cCI6IkpXVCJ9.eyJhcGlfa2V5IjoiNTM2M2U5ZjEtNmNjMS00MWNk
LWJiZTctN2U4NzFmMGFhZjJmIiwidGVuYW50X2lkIjoiY2I5OTE2NDk0MzFkN
WZmZWRmNmFiYzViMGFlOTIwYzFhZDRjMGY5MTg1ZjZiNDY0OTc3MzgyN2IyMz
npm i prisma
npx prisma init
DATABASE_URL="postgres://avnadmin:password@host/db"
name = "backend"
compatibility_date = "2023-12-01"
[vars]
DATABASE_URL = "prisma://accelerate.prisma-data.net/?api_key=
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhcGlfa2V5IjoiNTM2M2U5
ZjEtNmNjMS00MWNkLWJiZTctN2U4NzFmMGFhZjJmIiwidGVuYW50X2lkIjoiY
2I5OTE2NDk0MzFkNWZmZWRmNmFiYzViMGFlOTIwYzFhZDRjMGY5MTg1ZjZiND
Y0OTc3MzgyN2IyMzY2OWIwMiIsImludGVybmFsX3NlY3JldCI6Ijc0NjE4YWY
2LTA4NmItNDM0OC04MzIxLWMyMmY2NDEwOTExNyJ9.HXnE3vZjf8YH71uOoll
svrV-TSe41770FPG_O8IaVgs"
💡
You should not have your prod URL committed either in .env or in wrangler.toml to
github
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model User {
id String @id @default(uuid())
email String @unique
name String?
password String
posts Post[]
}
model Post {
id String @id @default(uuid())
title String
content String
published Boolean @default(false)
author User @relation(fields: [authorId], reference
s: [id])
authorId String
}
💡
You might face issues here, try changing your wifi if that happens
Solution
💡
Ideally you shouldn’t store passwords in plaintext. You should hash before storing
them. More details on how you can do that -
https://community.cloudflare.com/t/options-for-password-
hashing/138077https://developers.cloudflare.com/workers/runtime-apis/web-
crypto/
Solution
Step 6 - Middlewares
Creating a middleware in hono is well documented -
https://hono.dev/guides/middleware
Solution
Send the Header from Postman and ensure that the user id gets logged on the
server
Ref https://stackoverflow.com/questions/75554786/use-cloudflare-worker-env-
outside-fetch-scope
Hono let’s you group routes together so you can have a cleaner file structure.
Create two new files -
routes/user.ts
routes/blog.ts
index.ts
user.ts
Blog routes
Try to hit the routes via POSTMAN and ensure they work as expected
and c.set
You need to make typescript aware of the variables that you will be setting on the
context.
💡
Make sure you have logged in the cloudflare cli using npx wrangler login
1. Backend
2. Frontend
3. common
commonwill contain all the things that frontend and backend want to share.
We will make
common an independent npm module for now.
mkdir common
1. Update tsconfig.json
"rootDir": "./src",
"outDir": "./dist",
"declaration": true,
{
"name": "@100xdevs/common-app",
"version": "1.0.0",
"description": "",
"main": "dist/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
2. Install zod
a. signupInput / SignupInput
b. signinInput / SigninInput
c. createPostInput / CreatePostInput
d. updatePostInput / UpdatePostInput
Solution
2. Publish to npm
cd backend
npm i your_package_name
cd node_modules/your_package_name
Solution
1. Initialise tailwind
https://tailwindcss.com/docs/guides/vite
1. Update tailwind.config.js
1. Update index.css
@tailwind base;
1. Empty up App.css
npm i your_package
npm i react-router-dom
2. Add routing (ensure you create the Signup, Signin and Blog components)
function App() {
return (
<>
<BrowserRouter>
<Routes>
<Route path="/signup" element={<Signup />} />