-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
85 lines (74 loc) · 1.98 KB
/
index.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
const express = require("express");
const cors = require("cors");
const mongoose = require("mongoose");
require("dotenv").config();
const axios = require("axios");
const app = express();
app.use(cors());
app.use(express.json());
mongoose.connect(process.env.LINKMONGO);
app.get("/", (req, res) => {
res.status(200).json({ message: "Welcome on Mihoub's API" });
});
app.get("/comics", (req, res) => {
try {
axios
.get(
`https://lereacteur-marvel-api.herokuapp.com/comics/?apiKey=${
process.env.API_KEY
}&skip=${(req.query.page - 1) * 100}`
)
.then((response) => {
const comics = response.data;
console.log(response.data);
res.json(comics);
});
} catch (error) {
console.log(error.message);
}
});
app.get("/characters", (req, res) => {
try {
axios
.get(
`https://lereacteur-marvel-api.herokuapp.com/characters?apiKey=${
process.env.API_KEY
}&name=${req.query.name}&skip=${(req.query.page - 1) * 100}`
)
.then((response) => {
const characters = response.data;
res.json(characters);
});
} catch (error) {
console.log(error.message);
}
});
app.get("/characters/:id", async (req, res) => {
try {
const response = await axios.get(
`https://lereacteur-marvel-api.herokuapp.com/character/${req.params.id}?apiKey=${apiKey}`
);
res.json(response.data);
} catch (error) {
res.json(error.message);
}
});
app.get("/comics/:id", async (req, res) => {
try {
const response = await axios.get(
`https://lereacteur-marvel-api.herokuapp.com/comics/${req.params.id}?apiKey=${apiKey}`
);
res.json(response.data);
} catch (error) {
res.json(error.message);
}
});
const userRoutes = require("./routes/user");
app.use(userRoutes);
app.all("*", (req, res) => {
console.log("route not found");
res.status(404).json({ message: "route not found" });
});
app.listen(process.env.PORT, () => {
console.log("Go!");
});