-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
36 lines (30 loc) · 1.54 KB
/
app.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
const Express = require("express");
const app = Express();
const images = require("./images.json");
const cors = require("cors");
const favicon = require("serve-favicon");
app.use(favicon(__dirname + "/public/favicon.ico"));
app.use(cors());
app.use(Express.static(__dirname+'/public'));
app.get("/", (req, res) => {
res.status(200).json(images[Math.floor(Math.random() * images.length)]);
});
app.get("/resim", (req, res) => {
res.status(200).send(`<img style="width: 50%;" src="/images/${images[Math.floor(Math.random() * images.length)].id}.png" alt="ataturk"></img><style>body{background-color: red; margin: 0px; background: #0e0e0e; display: grid; place-items: center;</style>`);
});
app.get("/:id", (req, res) => {
if (images[(req.params.id)-1] === undefined){
res.status(404).json({"404-not-found": `Bu id'ye sahip bir resim yok yanlızca 1-${images.length} arasında bir sayı girilebilir.`})
}
res.status(200).json(images[(req.params.id)-1]);
});
app.get("/:id/resim", (req, res) => {
if (images[(req.params.id)-1] === undefined){
res.status(404).json({"404-not-found": `Bu id'ye sahip bir resim yok yanlızca 1-${images.length} arasında bir sayı girilebilir.`})
}
res.status(200).send(`<img style="width: 50%;" src="/images/${req.params.id}.png" alt="ataturk"></img><style>body{background-color: red; margin: 0px; background: #0e0e0e; display: grid; place-items: center;</style>`);
});
const PORT = process.env.PORT || 3002;
app.listen(PORT, () => {
console.warn(`App listening on http://localhost:${PORT}`);
});