-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
35 lines (27 loc) · 836 Bytes
/
index.js
File metadata and controls
35 lines (27 loc) · 836 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
const express = require('express')
const app = express()
app.get('/', function (req, res) {
res.send('Hello World')
})
// Exercício: Criar um endpoint [GET] /oi que exibe: "Olá, mundo!"
app.get('/oi', function (req, res) {
res.send('Olá, mundo!')
})
// Lista de Itens
const lista = ['Rick Sanchez', 'Morty Smith', 'Summer Smith']
// 0 1 2
// Endpoint Read All -> [GET] /item
app.get('/item', function (req, res) {
res.send(lista)
})
// Endpoint Read By ID -> [GET] /item/:id
app.get('/item/:id', function (req, res) {
// Acessamos o parâmetro de rota ID
const id = req.params.id
// Acessamos o item na lista (usando o id - 1)
// e colocamos na variável item
const item = lista[id - 1]
// Enviamos para a resposta o item acessado
res.send(item)
})
app.listen(3000)