Add initial implementation of Pokédex web application with HTML, CSS, JavaScript, and PHP backend

This commit is contained in:
vista-man
2025-03-11 13:52:23 +01:00
parent 2474c2527a
commit 59643b91c2
11 changed files with 443 additions and 3 deletions

42
school-pokedex/server.js Normal file
View File

@@ -0,0 +1,42 @@
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const port = 3000;
let pokemons = [
{ id: 1, name: 'Bulbasaur', type: 'Grass/Poison', image: 'https://img.pokemondb.net/artwork/bulbasaur.jpg' },
// ...add more initial Pokémon data here...
];
app.use(bodyParser.json());
app.use(express.static('school-pokedex'));
app.get('/api/pokemons', (req, res) => {
res.json(pokemons);
});
app.get('/api/pokemons/:id', (req, res) => {
const pokemon = pokemons.find(p => p.id == req.params.id);
res.json(pokemon);
});
app.post('/api/pokemons', (req, res) => {
const newPokemon = { id: Date.now(), ...req.body };
pokemons.push(newPokemon);
res.status(201).json(newPokemon);
});
app.put('/api/pokemons/:id', (req, res) => {
const index = pokemons.findIndex(p => p.id == req.params.id);
pokemons[index] = { id: parseInt(req.params.id), ...req.body };
res.json(pokemons[index]);
});
app.delete('/api/pokemons/:id', (req, res) => {
pokemons = pokemons.filter(p => p.id != req.params.id);
res.status(204).end();
});
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});