-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
88 lines (69 loc) · 2.69 KB
/
index.js
File metadata and controls
88 lines (69 loc) · 2.69 KB
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
86
87
import express, { response } from "express";
import bodyParser from "body-parser";
import axios from "axios";
const app = express();
const port = 3000;
const PokemonURL = "https://pokeapi.co/api/v2"
app.use(express.static("public"));
app.use(bodyParser.urlencoded({extended: true}));
var pokemonName, pokemonPic;
app.get("/", (req, res) => {
res.render("index.ejs" , {pokemonName: pokemonName, pokemonPic: pokemonPic});
});
app.post("/search", async (req, res) => {
const pokemon = req.body.pokemon.trim()
//console.log(pokemon);
try {
//console.log(pokemon);
const response = await axios.get(`${PokemonURL}/pokemon?limit=10000`);
//console.log(response);
const data = response.data;
//console.log(data);
const allPokemonNames = data.results.map(pokemon => pokemon.name);
const allPokemonInfo = data.results.map(link => link.url);
const filteredPokemonNames = allPokemonNames.filter(name =>
name.includes(pokemon.toLowerCase())
);
var pokeIndex = []
filteredPokemonNames.forEach(e => {
pokeIndex.push(allPokemonNames.indexOf(e));
})
var filteredPokemonInfo = []
pokeIndex.forEach(index =>
filteredPokemonInfo.push(allPokemonInfo[index])
);
//console.log(filteredPokemonInfo);
if (filteredPokemonNames.length === 0){
res.render("index.ejs", {pokemonName: ['Not Found'], pokeInfo: ['Not Found']});
} else {
res.render("index.ejs", {pokemonName: filteredPokemonNames, pokeInfo: filteredPokemonInfo});
}
} catch (error){
console.log("Fail to Load. Try Again")
res.render("index.ejs", {pokemonName: ['PokeAPIs Not Responding! Please try again later.'], pokeInfo: ['Not Found']});
}
});
app.post("/select", async (req, res) => {
const pokemonInfo = req.body.info;
//console.log(pokemonInfo);
try {
const response = await axios.get(`${pokemonInfo}`);
const data = response.data;
const allMovesTypesURL = data.moves.map(link => link.move.url);
var moveTypes = [];
while(moveTypes.length < allMovesTypesURL.length){
let moveURL = await axios.get(`${allMovesTypesURL[moveTypes.length]}`)
let moveData = moveURL.data;
moveTypes.push(moveData.type.name);
//console.log(moveTypes);
}
//console.log(moveTypes);
res.render("pokemon.ejs", {pokemon: data, moveTypes: moveTypes})
} catch (error){
console.log("Fail to Load. Try Again")
res.redirect("/")
}
});
app.listen(port, () => {
console.log(`Server running on port: ${port}`);
});