Skip to content

Instantly share code, notes, and snippets.

@josecarneiro
Created November 12, 2019 09:32
Show Gist options
  • Save josecarneiro/4859694df9bad0432a839de6113fed61 to your computer and use it in GitHub Desktop.
Save josecarneiro/4859694df9bad0432a839de6113fed61 to your computer and use it in GitHub Desktop.
const http = require('http');
const Pokedex = require('pokedex');
const pokedex = new Pokedex();
const server = http.createServer((request, response) => {
const endpoint = request.url;
const pokemonName = endpoint.substring(1);
try {
const pokemon = pokedex.pokemon(pokemonName);
// console.log(pokemon);
const pokemonInformation = `The pokemon ${pokemon.name} is number ${pokemon.id} in the pokedex and weighs ${pokemon.weight} lbs`;
const pokemonImage = pokemon.sprites.animated;
const htmlResponse = `<h3>${pokemonInformation}</h3><img src="${pokemonImage}" />`;
response.setHeader('Content-Type', 'text/html');
response.write(htmlResponse);
} catch (error) {
response.write("That pokemon couldn't be found");
}
response.end();
});
server.listen(3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment