node.js Parte I Prática Flashcards

(19 cards)

1
Q

Como criar um servidor básico usando Express?

A

const express = require(“express”)
const app = express()

app.listen(3000)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Como permitir que o Express leia JSON no body?

A

app.use(express.json())

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Como criar uma rota GET chamada /usuarios?

A

app.get(“/usuarios”, (req, res) => {
res.json([])
})

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Como acessar um parâmetro de rota :id?

A

req.params.id

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Como retornar status 404 no Express?

A

res.status(404).json({ erro: “Não encontrado” })

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Como pegar dados enviados no body de uma requisição POST?

A

const { nome, idade } = req.body

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Como encontrar um item em um array pelo ID?

A

array.find(item => item.id === id)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Como remover um item de um array pelo ID?

A

array = array.filter(item => item.id !== id)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Como criar ID automático simples em array?

A

id: array.length + 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Como criar um middleware no Express?

A

app.use((req, res, next) => {
console.log(“Passou aqui”)
next()
})

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

O que acontece se não chamar next() em middleware?

A

A requisição trava e não chega na rota.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Como usar async/await em rota Express?

A

app.get(“/”, async (req, res) => {
await algumaFuncao()
res.json({ ok: true })
})

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Como simular atraso de 2 segundos?

A

await new Promise(resolve => setTimeout(resolve, 2000))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Como exportar um router em arquivo separado?

A

module.exports = router

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Como importar uma rota externa no server?

A

const rota = require(“./routes/rota”)
app.use(“/rota”, rota)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Como adicionar item em array?

A

array.push(novoItem)

17
Q

Como verificar se item existe no array?

A

array.some(item => item.id === id)

18
Q

Como tratar erro com try/catch?

A

try {
// código
} catch (error) {
console.log(error)
}

19
Q

Como verificar se campo obrigatório não foi enviado?

A

if (!nome || !preco) {
return res.status(400).json({ erro: “Campos obrigatórios” })
}