-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
45 lines (38 loc) · 926 Bytes
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
const express = require("express");
const sqlite3 = require("sqlite3").verbose();
const app = express();
const port = 3000;
const db = new sqlite3.Database("./todo.db");
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.set("view engine", "pug");
app.get("/", (req, res) => {
let todolist = [];
db.each(
"SELECT name FROM todos",
(err, row) => {
if (err) {
console.log(err);
} else {
console.log(row);
todolist.push(row.name);
}
},
(err) => {
return res.render("index", {
title: "Hey",
message: "Hello there!",
todolist: todolist,
});
}
);
});
app.post("/", (req, res) => {
console.log(req.body);
let todo = req.body.todo;
db.run("INSERT INTO todos(name) VALUES(?)", todo);
res.redirect("/");
});
app.listen(port, () => {
console.log(`CS50 app listening on port ${port}`);
});