Initial commit: installable PWA hello world
React + TypeScript + Tailwind v4 + shadcn/ui frontend (client), Express + better-sqlite3 backend (server) in a pnpm workspace. Includes vite-plugin-pwa manifest, service worker, and iOS install meta tags. GET /api/hello increments a visit counter in SQLite; backend serves the built client in production.
This commit is contained in:
2
server/.env.example
Normal file
2
server/.env.example
Normal file
@@ -0,0 +1,2 @@
|
||||
# Port the backend listens on (defaults to 3001)
|
||||
PORT=3001
|
||||
24
server/package.json
Normal file
24
server/package.json
Normal file
@@ -0,0 +1,24 @@
|
||||
{
|
||||
"name": "server",
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"main": "dist/index.js",
|
||||
"scripts": {
|
||||
"dev": "tsx watch src/index.ts",
|
||||
"build": "tsc",
|
||||
"start": "node dist/index.js",
|
||||
"typecheck": "tsc --noEmit"
|
||||
},
|
||||
"dependencies": {
|
||||
"better-sqlite3": "^11.8.1",
|
||||
"express": "^4.21.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/better-sqlite3": "^7.6.13",
|
||||
"@types/express": "^5.0.0",
|
||||
"@types/node": "^24.13.3",
|
||||
"tsx": "^4.19.3",
|
||||
"typescript": "^5.7.3"
|
||||
}
|
||||
}
|
||||
35
server/src/db.ts
Normal file
35
server/src/db.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import Database from "better-sqlite3"
|
||||
import fs from "node:fs"
|
||||
import path from "node:path"
|
||||
import { fileURLToPath } from "node:url"
|
||||
|
||||
const __dirname = path.dirname(fileURLToPath(import.meta.url))
|
||||
|
||||
const dataDir = path.resolve(__dirname, "../data")
|
||||
fs.mkdirSync(dataDir, { recursive: true })
|
||||
|
||||
const dbPath = path.join(dataDir, "app.db")
|
||||
|
||||
export const db = new Database(dbPath)
|
||||
db.pragma("journal_mode = WAL")
|
||||
|
||||
db.exec(`
|
||||
CREATE TABLE IF NOT EXISTS visits (
|
||||
id INTEGER PRIMARY KEY CHECK (id = 1),
|
||||
count INTEGER NOT NULL DEFAULT 0
|
||||
)
|
||||
`)
|
||||
|
||||
const existing = db
|
||||
.prepare("SELECT count FROM visits WHERE id = 1")
|
||||
.get() as { count: number } | undefined
|
||||
|
||||
if (!existing) {
|
||||
db.prepare("INSERT INTO visits (id, count) VALUES (1, 0)").run()
|
||||
}
|
||||
|
||||
export function incrementAndGetVisits(): number {
|
||||
db.prepare("UPDATE visits SET count = count + 1 WHERE id = 1").run()
|
||||
const row = db.prepare("SELECT count FROM visits WHERE id = 1").get() as { count: number }
|
||||
return row.count
|
||||
}
|
||||
32
server/src/index.ts
Normal file
32
server/src/index.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import express from "express"
|
||||
import fs from "node:fs"
|
||||
import path from "node:path"
|
||||
import { fileURLToPath } from "node:url"
|
||||
|
||||
import { helloRouter } from "./routes/hello.js"
|
||||
|
||||
const __dirname = path.dirname(fileURLToPath(import.meta.url))
|
||||
|
||||
const app = express()
|
||||
const PORT = process.env.PORT ? Number(process.env.PORT) : 3001
|
||||
|
||||
app.use(express.json())
|
||||
|
||||
app.get("/api/health", (_req, res) => {
|
||||
res.json({ ok: true })
|
||||
})
|
||||
|
||||
app.use("/api", helloRouter)
|
||||
|
||||
// In production, serve the built client (client/dist) and fall back to index.html.
|
||||
const clientDist = path.resolve(__dirname, "../../client/dist")
|
||||
if (fs.existsSync(clientDist)) {
|
||||
app.use(express.static(clientDist))
|
||||
app.get("*", (_req, res) => {
|
||||
res.sendFile(path.join(clientDist, "index.html"))
|
||||
})
|
||||
}
|
||||
|
||||
app.listen(PORT, () => {
|
||||
console.log(`Pocket Pascal server listening on http://localhost:${PORT}`)
|
||||
})
|
||||
14
server/src/routes/hello.ts
Normal file
14
server/src/routes/hello.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { Router } from "express"
|
||||
|
||||
import { incrementAndGetVisits } from "../db.js"
|
||||
|
||||
export const helloRouter = Router()
|
||||
|
||||
helloRouter.get("/hello", (_req, res) => {
|
||||
const visits = incrementAndGetVisits()
|
||||
res.json({
|
||||
message: "Hello, World!",
|
||||
visits,
|
||||
ts: new Date().toISOString(),
|
||||
})
|
||||
})
|
||||
24
server/tsconfig.json
Normal file
24
server/tsconfig.json
Normal file
@@ -0,0 +1,24 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ES2022",
|
||||
"module": "NodeNext",
|
||||
"moduleResolution": "NodeNext",
|
||||
"lib": ["ES2023"],
|
||||
"types": ["node"],
|
||||
|
||||
"rootDir": "src",
|
||||
"outDir": "dist",
|
||||
"sourceMap": true,
|
||||
"declaration": false,
|
||||
|
||||
"strict": true,
|
||||
"esModuleInterop": true,
|
||||
"skipLibCheck": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"resolveJsonModule": true,
|
||||
"noUnusedLocals": true,
|
||||
"noUnusedParameters": true,
|
||||
"noFallthroughCasesInSwitch": true
|
||||
},
|
||||
"include": ["src"]
|
||||
}
|
||||
Reference in New Issue
Block a user