-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip: added basic node + client solutions
- Loading branch information
Paul Pacurar
committed
Sep 29, 2021
1 parent
ac11697
commit 9b6b057
Showing
34 changed files
with
29,488 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. | ||
|
||
# dependencies | ||
*/node_modules | ||
/.pnp | ||
.pnp.js | ||
|
||
# testing | ||
/coverage | ||
|
||
# production | ||
/build | ||
|
||
# misc | ||
.DS_Store | ||
.env.local | ||
.env.development.local | ||
.env.test.local | ||
.env.production.local | ||
|
||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"type": "pwa-node", | ||
"request": "launch", | ||
"name": "Launch Program", | ||
"skipFiles": [ | ||
"<node_internals>/**" | ||
], | ||
"program": "${workspaceFolder}/api/index.js" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import express from "express"; | ||
import Pokedex from "pokedex-promise-v2"; | ||
import fetch from "node-fetch"; | ||
const P = new Pokedex(); | ||
|
||
let app = express(); | ||
|
||
app.get("/api/pokemon/:id", async function (req, res) { | ||
P.getPokemonByName(req.params.id, function (response, error) { | ||
// with callback | ||
if (!error) { | ||
console.log("Got res: "); | ||
res.json(response); | ||
} else { | ||
console.log(error); | ||
res.json(error); | ||
} | ||
}); | ||
}); | ||
|
||
app.get("/api/generation", async function (req, res) { | ||
const response = await fetch("https://pokeapi.co/api/v2/generation"); | ||
const data = await response.json(); | ||
res.json(data.results); | ||
}); | ||
|
||
app.get("/api/pokemon", async function (req, res) { | ||
const { query } = req; | ||
const { generation, name, type } = query; | ||
if (name) { | ||
console.log("poke with name: ", name); | ||
P.getPokemonByName(req.params.id, function (response, error) { | ||
// with callback | ||
if (!error) { | ||
console.log("Got res on api/pokemon "); | ||
res.json(response); | ||
} else { | ||
console.log(error); | ||
} | ||
}); | ||
} else { | ||
const response = await fetch( | ||
`https://pokeapi.co/api/v2/generation/${generation}` | ||
); | ||
const data = await response.json(); | ||
const selectedData = generation === "all" ? data.pokemon_species : data; | ||
res.json(selectedData); | ||
} | ||
}); | ||
|
||
app.listen(5000, function () { | ||
console.log("Server is listening on port 5000"); | ||
}); |
Oops, something went wrong.