-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
39 lines (31 loc) · 957 Bytes
/
index.js
File metadata and controls
39 lines (31 loc) · 957 Bytes
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
const express = require('express');
const sunTzu = require('sun-tzu-quotes');
const swaggerUi = require("swagger-ui-express");
const swaggerDocument = require("./swagger.json");
const app = express();
app.get('/', (req, res) => {
res.redirect('/api-docs');
});
app.use('/api-docs', swaggerUi.serve);
app.get('/api-docs', swaggerUi.setup(swaggerDocument));
app.get("/quote", (req, res) => {
res.status(200).json({
"quote": sunTzu()
});
});
app.get("/quote/:id", (req, res) => {
const id = req.params.id;
if(id > 0 && id < sunTzu.quotes.length) {
res.status(200).json({
"quote": sunTzu.quotes[id]
});
} else {
res.status(404).json({
"error": "Quote not found"
});
}
});
app.get("/quotes", (req, res) => {
res.status(200).json(sunTzu.quotes);
});
app.listen(process.env.PORT, () => console.log(`Sun Tzu Quotes API listening on port ${process.env.PORT}`));