-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
69 lines (54 loc) · 1.71 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
import express from 'express'
import ejs from "ejs"
import path from "path"
import characters from './assets/characters.js';
const app = express();
const PORT = process.env.PORT ?? 4000
app.set('view engine', 'ejs')
//API CRUD
app.get("/api", (req,res)=>{
res.status(200).json(characters)
})
app.get("/api/:id", (req,res)=>{
const {id} = req.params
const charFound = characters.find(char => char.id === id)
if(charFound){
res.status(200).json(charFound)
}else{
res.status(404).send(`Personagem não encontrado, informação "${id}" incorreta`)
}
})
app.post("/api/create",(req,res)=>{
const {id, imgUrl, name, nickname,
crew, position, specialSkill,
dream, reward, food, phrase, birthday,
akumanomiUser, akumanomi}
= req.body;
const newCharacter = {id, imgUrl, name, nickname,
crew, position, specialSkill,
dream, reward, food, phrase, birthday,
akumanomiUser, akumanomi}
characters.push(newCharacter);
res.status(201).json({message:`personagen ${name} adicionado com sucesso!`})
})
//HTML Base
app.get("/",(req,res)=>{
res.status(200).sendFile(path.resolve('./htmlPages/instructions.html'))
})
//Graphic CRUD
app.get("/all",(req, res)=>{
res.status(200).render('allChar', {characters:characters})
})
app.get("/char/:id", (req,res)=>{
const {id} = req.params
const charFound = characters.find(char => char.id === id)
if(charFound){
res.status(200).render('oneChar',{char:charFound})
}else{
res.status(404).send(`Personagem não encontrado, informação "${id}" incorreta`)
}
})
app.listen(PORT, ()=>{
console.log("Straw Hat API na porta "+PORT)
})
export default app;