Quando faço qualquer requisição pelo postman aparece o erro
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>SyntaxError: Unexpected end of JSON input<br> at JSON.parse (<anonymous>)<br> at createStrictSyntaxError (C:\Alura\Formacoes\4-nodeJS\API-rest\node_modules\body-parser\lib\types\json.js:160:10)<br> at parse (C:\Alura\Formacoes\4-nodeJS\API-rest\node_modules\body-parser\lib\types\json.js:83:15)<br> at C:\Alura\Formacoes\4-nodeJS\API-rest\node_modules\body-parser\lib\read.js:128:18<br> at AsyncResource.runInAsyncScope (node:async_hooks:202:9)<br> at invokeCallback (C:\Alura\Formacoes\4-nodeJS\API-rest\node_modules\raw-body\index.js:231:16)<br> at done (C:\Alura\Formacoes\4-nodeJS\API-rest\node_modules\raw-body\index.js:220:7)<br> at IncomingMessage.onEnd (C:\Alura\Formacoes\4-nodeJS\API-rest\node_modules\raw-body\index.js:280:7)<br> at IncomingMessage.emit (node:events:539:35)<br> at endReadableNT (node:internal/streams/readable:1345:12)</pre>
</body>
</html>
Mas pelo navegador funciona, acho que tem algum erro que não consigo perceber :( Meu código
import express from "express";
const app = express();
app.use(express.json())
const books = [
{id: 1, "title":"Senhor dos Anéis"},
{id: 2, "title":"O Hobbit"}
]
app.get('/', (req, res) => {
res.status(200)
.send("Curso de node");
})
app.get('/livros', (req, res) => {
res.status(200)
.json(books);
})
app.get('/livros/:id', (req, res) => {
let index = searchBook(req.params.id)
res.json(books[index])
})
app.post('/livros', (req, res) => {
books.push(req.body);
res.status(201)
.send('Livro cadastrado com sucesso')
})
app.put('/livros/:id', (req, res) => {
let index = searchBook(req.params.id);
books[index].title = req.body.title;
res.json(books)
})
function searchBook (id){
return books.findIndex(book => book.id == id);
}
export default app