init
This commit is contained in:
63
src/index.ts
Normal file
63
src/index.ts
Normal file
@ -0,0 +1,63 @@
|
||||
import express from "express";
|
||||
import ejs from "ejs";
|
||||
import multer from "multer";
|
||||
|
||||
const app = express();
|
||||
const port = 8080;
|
||||
const upload = multer();
|
||||
|
||||
let people = [
|
||||
{
|
||||
id: 1,
|
||||
name: "Adam",
|
||||
secret: "adam"
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
name: "Vojta",
|
||||
secret: "vojta"
|
||||
},
|
||||
]
|
||||
|
||||
//app.use(express.urlencoded);
|
||||
app.use(express.static('www'));
|
||||
|
||||
app.get("/", (req, res) => {
|
||||
ejs.renderFile('src/templates/index.ejs', { people: people }, function (err, str) {
|
||||
if (err) {
|
||||
res.status(500).send(err);
|
||||
}
|
||||
|
||||
res.send(str);
|
||||
});
|
||||
});
|
||||
|
||||
app.get("/gamble", (req, res) => {
|
||||
ejs.renderFile('src/templates/gamble.ejs', { people: people }, function (err, str) {
|
||||
if (err) {
|
||||
res.status(500).send(err);
|
||||
}
|
||||
|
||||
res.send(str);
|
||||
});
|
||||
});
|
||||
|
||||
app.post("/gamble", upload.none(), (req, res) => {
|
||||
console.log(req.body);
|
||||
res.redirect('/');
|
||||
});
|
||||
|
||||
app.get("/person/:id", (req, res) => {
|
||||
let person = people.find((elem) => elem.id == req.params.id);
|
||||
ejs.renderFile('src/templates/person.ejs', { person: person }, function (err, str) {
|
||||
if (err) {
|
||||
res.status(500).send(err);
|
||||
}
|
||||
|
||||
res.send(str);
|
||||
});
|
||||
});
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`Listening on port ${port}...`);
|
||||
});
|
||||
21
src/templates/gamble.ejs
Normal file
21
src/templates/gamble.ejs
Normal file
@ -0,0 +1,21 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width" />
|
||||
<link rel="stylesheet" href="/bootstrap.min.css" />
|
||||
<script src="/bootstrap.bundle.min.css"></script>
|
||||
<title>Index</title>
|
||||
</head>
|
||||
|
||||
<body data-bs-theme="dark">
|
||||
<form action="/gamble" method="post" enctype="multipart/form-data">
|
||||
<label for="fname">First name:</label><br>
|
||||
<input type="text" id="fname" name="fname" value="John"><br>
|
||||
<label for="lname">Last name:</label><br>
|
||||
<input type="text" id="lname" name="lname" value="Doe"><br><br>
|
||||
<input type="submit" value="Submit">
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
||||
29
src/templates/index.ejs
Normal file
29
src/templates/index.ejs
Normal file
@ -0,0 +1,29 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width" />
|
||||
<link rel="stylesheet" href="/bootstrap.min.css" />
|
||||
<script src="/bootstrap.bundle.min.css"></script>
|
||||
<title>Index</title>
|
||||
</head>
|
||||
|
||||
<body data-bs-theme="dark">
|
||||
<a href="/gamble">gamble</a>
|
||||
People
|
||||
<div class="row p-5">
|
||||
<div class="list-group col-3">
|
||||
<% people.forEach((person) => {%>
|
||||
<a href='/person/<%= person.id %>'
|
||||
class="list-group-item list-group-item-action">
|
||||
<div class="me-auto">
|
||||
<span class="fw-bold"><%= person.name %></span>
|
||||
<%= person.id %>
|
||||
</div>
|
||||
</a>
|
||||
<% }); %>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
25
src/templates/person.ejs
Normal file
25
src/templates/person.ejs
Normal file
@ -0,0 +1,25 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width" />
|
||||
<link rel="stylesheet" href="/bootstrap.min.css" />
|
||||
<script src="/bootstrap.bundle.min.css"></script>
|
||||
<title>Person <%= person.name %></title>
|
||||
</head>
|
||||
|
||||
<body data-bs-theme="dark">
|
||||
<a href="/">Index</a>
|
||||
<a href="/gamble">gamble</a>
|
||||
<p>
|
||||
Name: <%= person.name %>
|
||||
</p>
|
||||
<p>
|
||||
Id: <%= person.id %>
|
||||
</p>
|
||||
<p>
|
||||
secret: <%= person.secret %>
|
||||
</p>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user