-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
80 lines (70 loc) · 1.94 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
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
const express = require("express");
const https = require("https");
const bodyParser = require("body-parser");
const ejs = require("ejs");
// var _ = require('lodash');
var Pokedex = require('pokedex-promise-v2');
var P = new Pokedex();
let pokemonAbilities = [];
let image;
let types = [];
let pokemonName;
let weight;
let height;
let moves = [];
let hp;
const app = express();
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(express.static("public"));
app.get("/", function (req, res) {
res.render("pokemon");
});
app.post("/", function(req, res){
pokemonName = (req.body.pokemon).toLowerCase();
pokemonAbilities = [];
types = [];
moves = [];
P.getPokemonByName(pokemonName) // with Promise
.then(function (response) {
const listOfAbilities = response.abilities;
listOfAbilities.forEach(ability =>
pokemonAbilities.push(ability.ability.name)
)
image = response.sprites.front_default;
const listOfTypes = response.types;
listOfTypes.forEach(type =>
types.push(type.type.name)
)
weight = response.weight;
height = response.height;
const listOfMoves = response.moves.slice(0,5);
listOfMoves.forEach(move =>
moves.push(move.move.name))
hp = response.stats[0].base_stat
res.redirect("/pokemonCard");
})
.catch(function (error) {
res.write("<h1>please try again</h1>");
});
})
app.get("/pokemonCard", function (req, res){
res.render("pokemonCard", {
abilities: pokemonAbilities,
pokemonImage: image,
types: types,
pokemonName: pokemonName,
weight: weight,
height: height,
moves: moves,
hp: hp
})
})
app.post("/pokemonCard", function(req,res) {
res.redirect("/");
})
app.listen(process.env.PORT || 3000, function () {
console.log("server running on port 3000.");
});