This commit is contained in:
2024-09-13 17:08:31 +02:00
parent 4272f14ebf
commit 6a65074c16
17 changed files with 797 additions and 26 deletions

175
server/.gitignore vendored Normal file
View File

@@ -0,0 +1,175 @@
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore
# Logs
logs
_.log
npm-debug.log_
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*
# Caches
.cache
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
# Runtime data
pids
_.pid
_.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
*.lcov
# nyc test coverage
.nyc_output
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# Snowpack dependency directory (https://snowpack.dev/)
web_modules/
# TypeScript cache
*.tsbuildinfo
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional stylelint cache
.stylelintcache
# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local
# parcel-bundler cache (https://parceljs.org/)
.parcel-cache
# Next.js build output
.next
out
# Nuxt.js build / generate output
.nuxt
dist
# Gatsby files
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public
# vuepress build output
.vuepress/dist
# vuepress v2.x temp and cache directory
.temp
# Docusaurus cache and generated files
.docusaurus
# Serverless directories
.serverless/
# FuseBox cache
.fusebox/
# DynamoDB Local files
.dynamodb/
# TernJS port file
.tern-port
# Stores VSCode versions used for testing VSCode extensions
.vscode-test
# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
# IntelliJ based IDEs
.idea
# Finder (MacOS) folder config
.DS_Store

15
server/README.md Normal file
View File

@@ -0,0 +1,15 @@
# server
To install dependencies:
```bash
bun install
```
To run:
```bash
bun run index.ts
```
This project was created using `bun init` in bun v1.1.27. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime.

BIN
server/bun.lockb Normal file

Binary file not shown.

19
server/dashboard.html Normal file
View File

@@ -0,0 +1,19 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta
name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"
/>
<meta
http-equiv="X-UA-Compatible"
content="ie=edge"
/>
<title>{{username}} | Dashboard</title>
</head>
<body>
<h1>Hello, {{username}}!</h1>
You are <strong>{{age}}</strong> and beautiful!
</body>
</html>

64
server/index.html Normal file
View File

@@ -0,0 +1,64 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta
name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"
/>
<meta
http-equiv="X-UA-Compatible"
content="ie=edge"
/>
<title>Sign In</title>
</head>
<style>
:root {
color-scheme: dark light;
font-family: 'Inter', sans-serif;
}
</style>
<body>
<form>
<div>
<label for="email-input">Email</label>
<input
name="email"
id="email-input"
placeholder="Email"
type="email"
/>
</div>
<div>
<label for="password-input">Password</label>
<input
name="password"
id="password-input"
placeholder="Password"
/>
</div>
<div>
<label for="checkbox-input">Checkbox</label>
<input
required
name="checkbox"
type="checkbox"
id="checkbox-input"
value="true"
/>
</div>
<button type="button">Not submit</button>
<button>Submit</button>
</form>
</body>
<script>
const form = document.querySelector('form')
form.addEventListener('submit', (e) => {
e.preventDefault()
const formObj = Object.fromEntries(new FormData(form))
console.log(formObj)
})
</script>
</html>

57
server/index.ts Normal file
View File

@@ -0,0 +1,57 @@
const users = [
{
name: 'Andrei',
age: '39',
email: 'andres@gmail.com',
password: 'pass',
},
{
name: 'Katya',
age: '18',
email: 'katya@gmail.com',
password: 'another-pass',
},
]
Bun.serve({
port: 3333,
static: {
'/': new Response(await Bun.file('./index.html').bytes(), {
headers: {
'Content-Type': 'text/html',
},
}),
},
async fetch(req) {
const url = new URL(req.url)
if (url.pathname === '/login' && req.method === 'GET') {
const email = url.searchParams.get('email')
const password = url.searchParams.get('password')
const user = users.find(
(u) => u.email === email && u.password === password
)
if (!user) {
return new Response(await Bun.file('./login-incorrect.html').bytes(), {
headers: {
'Content-Type': 'text/html',
},
})
}
const file = await Bun.file('./dashboard.html').text()
return new Response(
file
.replaceAll('{{username}}', user.name)
.replaceAll('{{age}}', user.age),
{
headers: {
'Content-Type': 'text/html',
},
}
)
}
if (url.pathname === '/blog') return new Response('Blog!')
return new Response('404!')
},
})

View File

@@ -0,0 +1,21 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta
name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"
/>
<meta
http-equiv="X-UA-Compatible"
content="ie=edge"
/>
<title>Incorrect login</title>
</head>
<body>
<h1>
Your username or password were incorrect, please try again by
<a href="/login">clicking here</a>
</h1>
</body>
</html>

14
server/package.json Normal file
View File

@@ -0,0 +1,14 @@
{
"name": "server",
"module": "index.ts",
"type": "module",
"devDependencies": {
"@types/bun": "latest"
},
"scripts": {
"dev": "bun --watch run index.ts"
},
"peerDependencies": {
"typescript": "^5.0.0"
}
}

27
server/tsconfig.json Normal file
View File

@@ -0,0 +1,27 @@
{
"compilerOptions": {
// Enable latest features
"lib": ["ESNext", "DOM"],
"target": "ESNext",
"module": "ESNext",
"moduleDetection": "force",
"jsx": "react-jsx",
"allowJs": true,
// Bundler mode
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"verbatimModuleSyntax": true,
"noEmit": true,
// Best practices
"strict": true,
"skipLibCheck": true,
"noFallthroughCasesInSwitch": true,
// Some stricter flags (disabled by default)
"noUnusedLocals": false,
"noUnusedParameters": false,
"noPropertyAccessFromIndexSignature": false
}
}