-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.ts
44 lines (37 loc) · 1.03 KB
/
app.ts
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
import express, { Request, Response, NextFunction } from "express";
import fs from "fs";
const app = express();
const PORT = 3000;
app.use(express.json());
app.set("view engine", "pug");
app.get("/books", (req: Request, res: Response, next: NextFunction) => {
try {
const { query } = req.query;
const books = getBooks();
if (!query) {
res.render("books", { filteredBooks: books });
} else {
const filteredBooks = books.filter((book) =>
book.name.toLowerCase().startsWith(query.toString().toLowerCase())
);
res.render("books", { filteredBooks });
}
} catch (error) {
next(error);
}
});
app.use((err: Error, req: Request, res: Response, next: NextFunction) => {
console.error(err.stack);
res.status(500).json({ error: "Something wrong" });
});
app.listen(3000, () => {
console.log(`server is running at ${PORT} `);
});
const getBooks = (): any[] => {
try {
const data = fs.readFileSync("books.json", "utf8");
return JSON.parse(data);
} catch (err) {
return [];
}
};