|
1 |
| -require("./tracing"); |
2 |
| -const fs = require("fs"); |
3 |
| -const express = require("express"); |
4 |
| -const cors = require("cors"); |
5 |
| -const { v4: uuidv4 } = require("uuid"); |
6 |
| -const morgan = require("morgan"); |
| 1 | +require('./tracing') |
| 2 | +const fs = require('fs') |
| 3 | +const express = require('express') |
| 4 | +const cors = require('cors') |
| 5 | +const { v4: uuidv4 } = require('uuid') |
| 6 | +const morgan = require('morgan') |
7 | 7 |
|
8 |
| -const app = express(); |
9 |
| -const port = process.env.PORT || 5000; |
| 8 | +const app = express() |
| 9 | +const port = process.env.PORT || 5000 |
10 | 10 |
|
11 |
| -app.use(express.json()); |
12 |
| -app.use(cors()); |
13 |
| -app.use(morgan("tiny")); |
| 11 | +app.use(express.json()) |
| 12 | +app.use(cors()) |
| 13 | +app.use(morgan('tiny')) |
14 | 14 |
|
15 |
| -app.get("/404", (req, res) => { |
16 |
| - res.status(404).sendFile(__dirname + "/404.html"); |
17 |
| -}); |
| 15 | +app.get('/404', (req, res) => { |
| 16 | + res.status(404).sendFile(__dirname + '/404.html') |
| 17 | +}) |
18 | 18 |
|
19 |
| -app.use(express.static(__dirname + "/vue/dist")); |
| 19 | +app.use(express.static(__dirname + '/vue/dist')) |
20 | 20 |
|
21 |
| -app.get("/api/books", (req, res) => { |
22 |
| - if (fs.existsSync("downtime.json")) { |
23 |
| - console.log('Downtime detected'); |
24 |
| - res.status(500).send(); |
25 |
| - } else { |
26 |
| - const rawData = fs.readFileSync("books.json"); |
27 |
| - const books = JSON.parse(rawData); |
| 21 | +app.get('/api/books', (req, res) => { |
| 22 | + if (fs.existsSync('downtime.json')) { |
| 23 | + console.log('Downtime detected') |
| 24 | + res.status(500).send() |
| 25 | + } else { |
| 26 | + const rawData = fs.readFileSync('books.json') |
| 27 | + const books = JSON.parse(rawData) |
28 | 28 |
|
29 |
| - res.status(200).json(books); |
30 |
| - } |
31 |
| -}); |
| 29 | + res.status(200).json(books) |
| 30 | + } |
| 31 | +}) |
32 | 32 |
|
33 |
| -app.get("/api/books/:id", (req, res) => { |
34 |
| - const rawData = fs.readFileSync("books.json"); |
35 |
| - const books = JSON.parse(rawData); |
36 |
| - var arrayFound = books.filter(function (item) { |
37 |
| - return item.id === req.params.id; |
38 |
| - }); |
39 |
| - res.status(200).json(arrayFound[0]); |
40 |
| -}); |
| 33 | +app.get('/api/books/:id', (req, res) => { |
| 34 | + const rawData = fs.readFileSync('books.json', 'utf8') |
| 35 | + const books = JSON.parse(rawData) |
| 36 | + const idAsNumber = Number(req.params.id) |
| 37 | + const found = books.find((item) => item.id === idAsNumber) |
| 38 | + res.status(200).json(found) |
| 39 | +}) |
41 | 40 |
|
42 |
| -app.get("/api/users/login", (req, res) => { |
43 |
| - res.status(200).json({ |
44 |
| - message: "Login successful", |
45 |
| - token: uuidv4(), |
46 |
| - name: "Danube", |
47 |
| - }); |
48 |
| -}); |
| 41 | +app.get('/api/users/login', (req, res) => { |
| 42 | + res.status(200).json({ |
| 43 | + message: 'Login successful', |
| 44 | + token: uuidv4(), |
| 45 | + name: 'Danube', |
| 46 | + }) |
| 47 | +}) |
49 | 48 |
|
50 |
| -app.post("/api/toggle", (req, res) => { |
51 |
| - if (fs.existsSync("downtime.json")) { |
52 |
| - fs.unlinkSync("downtime.json"); |
53 |
| - res.status(200).send(); |
54 |
| - } else { |
55 |
| - fs.writeFileSync("downtime.json", "{}"); |
56 |
| - res.status(200).send(); |
57 |
| - } |
58 |
| -}); |
| 49 | +app.post('/api/toggle', (req, res) => { |
| 50 | + if (fs.existsSync('downtime.json')) { |
| 51 | + fs.unlinkSync('downtime.json') |
| 52 | + res.status(200).send() |
| 53 | + } else { |
| 54 | + fs.writeFileSync('downtime.json', '{}') |
| 55 | + res.status(200).send() |
| 56 | + } |
| 57 | +}) |
59 | 58 |
|
60 | 59 | app.get('/api/downtime', (req, res) => {
|
61 |
| - const downtime = fs.existsSync('downtime.json') |
62 |
| - res.status(200).json({ downtime }) |
63 |
| -}); |
| 60 | + const downtime = fs.existsSync('downtime.json') |
| 61 | + res.status(200).json({ downtime }) |
| 62 | +}) |
64 | 63 |
|
65 | 64 | app.get(/.*/, (req, res) => {
|
66 |
| - res.sendFile(__dirname + "/vue/dist/index.html"); |
67 |
| -}); |
| 65 | + res.sendFile(__dirname + '/vue/dist/index.html') |
| 66 | +}) |
68 | 67 |
|
69 |
| -app.listen(port, () => console.log(`Example app listening on port ${port}!`)); |
| 68 | +app.listen(port, () => console.log(`Example app listening on port ${port}!`)) |
0 commit comments