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