Skip to content

Commit

Permalink
wip: added basic node + client solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Pacurar committed Sep 29, 2021
1 parent ac11697 commit 9b6b057
Show file tree
Hide file tree
Showing 34 changed files with 29,488 additions and 0 deletions.
23 changes: 23 additions & 0 deletions .gitignore
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*
17 changes: 17 additions & 0 deletions .vscode/launch.json
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"
}
]
}
53 changes: 53 additions & 0 deletions api/index.js
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");
});
Loading

0 comments on commit 9b6b057

Please sign in to comment.