Rozdíly
Zde můžete vidět rozdíly mezi vybranou verzí a aktuální verzí dané stránky.
Následující verze | Předchozí verze | ||
krouzek:expressjs [2023/10/31 20:59] – vytvořeno admin | krouzek:expressjs [2023/11/15 20:54] (aktuální) – upraveno mimo DokuWiki 127.0.0.1 | ||
---|---|---|---|
Řádek 1: | Řádek 1: | ||
+ | ==== Úkol ==== | ||
+ | * Vyzkoušejte si vytvořit vlastní ExpressJS aplikaci ([[krouzek: | ||
+ | * Sami si najděte '' | ||
+ | * **Jak správně strukturovat ExpressJS projekt? Představte si, že bude mít endpointů třeba 100!** - viz návod (2) | ||
+ | * Teď ukládáme všechna data čistě do paměti (in-memory). Jaká je hlavní nevýhoda? Na jakou aplikaci je to v pořádku využít a kde by to již vadilo? | ||
+ | * Kam budeme směřovat? **Databáze**! | ||
+ | |||
+ | |||
+ | ===== (1) ExpressJS aplikace ===== | ||
+ | |||
> Write a step-by-step tutorial how to create an ExpressJS application with two example routes, GET and POST interacting via JSON. Make that a POST game/ to create new game and GET game/:id to query the game state. | > Write a step-by-step tutorial how to create an ExpressJS application with two example routes, GET and POST interacting via JSON. Make that a POST game/ to create new game and GET game/:id to query the game state. | ||
Řádek 47: | Řádek 57: | ||
Create a POST route to create a new game. This route will accept JSON data and generate a unique game ID. | Create a POST route to create a new game. This route will accept JSON data and generate a unique game ID. | ||
- | <code javascript> | + | <code javascript> |
- | app.use(express.json()); | + | |
app.post('/ | app.post('/ | ||
- | const newGameId = gameIdCounter++; | + | |
- | const newGame = { | + | const newGame = { |
- | id: newGameId, | + | id: newGameId, |
- | state: ' | + | state: ' |
- | }; | + | } |
- | games[newGameId] = newGame; | + | games[newGameId] = newGame |
- | res.status(201).json(newGame); | + | res.status(201).json(newGame) |
- | }); | + | }) |
</ | </ | ||
Create a GET route to query the game state by ID. | Create a GET route to query the game state by ID. | ||
- | <code javascript> | + | <code javascript> |
- | app.get('/ | + | const gameId = parseInt(req.params.id) |
- | const gameId = parseInt(req.params.id); | + | const game = games[gameId] |
- | const game = games[gameId]; | + | if (game) { |
- | if (game) { | + | res.json(game) |
- | res.json(game); | + | } else { |
- | } else { | + | res.status(404).json({ error: 'Game not found' }) |
- | res.status(404).json({ error: 'Game not found' }); | + | } |
- | } | + | }) |
- | }); | + | |
</ | </ | ||
Řádek 80: | Řádek 88: | ||
<code javascript> | <code javascript> | ||
app.listen(port, | app.listen(port, | ||
- | echo " | + | |
}); | }); | ||
</ | </ | ||
Řádek 100: | Řádek 108: | ||
That's it! You've created an Express.js application with two example routes (GET and POST) that interact via JSON to manage a simple game state. You can expand upon this foundation to build more complex applications with Express.js. | That's it! You've created an Express.js application with two example routes (GET and POST) that interact via JSON to manage a simple game state. You can expand upon this foundation to build more complex applications with Express.js. | ||
+ | |||
+ | |||
+ | ---- | ||
+ | ===== (2) Strukturování ExpressJS aplikace ===== | ||
+ | |||
+ | > Write a beginner guide to structure a ExpressJS project. Explain idea of controller and model. Mention view but do not elaborate, as we're about to create API application only. In which folders to put the source files? Show example route file and controller. | ||
+ | |||
+ | ===== Project Structure ===== | ||
+ | |||
+ | **Project Root**: This is where your main application file (usually '' | ||
+ | ** | ||
+ | Folders**: | ||
+ | * routes: Contains route files, where you define the endpoints and their corresponding controllers. | ||
+ | * controllers: | ||
+ | * models: Contains model files, which represent the data structure and interact with your database. Since you're building an API application, | ||
+ | |||
+ | Here's a simplified example of how to structure an Express.js project: | ||
+ | |||
+ | < | ||
+ | my-express-app/ | ||
+ | |-- app.js | ||
+ | |-- routes/ | ||
+ | | |-- gameRoutes.js | ||
+ | |-- controllers/ | ||
+ | | |-- gameController.js | ||
+ | |-- models/ | ||
+ | | |-- gameModel.js | ||
+ | </ | ||
+ | |||
+ | ===== Controllers and Models ===== | ||
+ | |||
+ | **Controllers**: | ||
+ | |||
+ | **Models**: Models define the structure and behavior of your data. In an API application, | ||
+ | |||
+ | ===== Example Route File (gameRoutes.js) ===== | ||
+ | <code javascript> | ||
+ | const express = require(' | ||
+ | const router = express.Router(); | ||
+ | const gameController = require(' | ||
+ | |||
+ | // Define routes and their corresponding controller functions | ||
+ | router.post('/ | ||
+ | router.get('/ | ||
+ | |||
+ | module.exports = router; | ||
+ | </ | ||
+ | |||
+ | ===== Example Controller (gameController.js) ===== | ||
+ | <code javascript> | ||
+ | const games = {}; // In-memory storage for game data | ||
+ | let gameIdCounter = 1; | ||
+ | |||
+ | const createGame = (req, res) => { | ||
+ | const newGameId = gameIdCounter++; | ||
+ | const newGame = { | ||
+ | id: newGameId, | ||
+ | state: ' | ||
+ | }; | ||
+ | games[newGameId] = newGame; | ||
+ | res.status(201).json(newGame); | ||
+ | }; | ||
+ | |||
+ | const getGame = (req, res) => { | ||
+ | const gameId = parseInt(req.params.id); | ||
+ | const game = games[gameId]; | ||
+ | if (game) { | ||
+ | res.json(game); | ||
+ | } else { | ||
+ | res.status(404).json({ error: 'Game not found' }); | ||
+ | } | ||
+ | }; | ||
+ | |||
+ | module.exports = { | ||
+ | createGame, | ||
+ | getGame, | ||
+ | }; | ||
+ | </ | ||
+ | |||
+ | In this example, the '' | ||
+ | |||
+ | This structure provides separation of concerns, making your project more organized and easier to maintain as it grows. The controller handles the application' | ||
+ | |||
+ | ----- | ||
+ | |||
+ | |||
+ |