JavaScript

Muhammad Ali
Muhammad Ali
5,957 Points

Post is not working

Response

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot POST /qoutes</pre>
</body>
</html>

Code

const express = require('express');
const app = express();
const records=require('./records.js')
app.use(express.json());
app.get('/qoutes', (req, res) => {
    records.getQuotes().then(
        (qoutes) => {
            res.json(qoutes)
        }
    );

})
app.get('/qoute/:id', (req, res) => {
    records.getQuote(req.params.id).then((qoute) => {
        res.json(qoute)
    })
    app.post('/qoutes', async (req, res) => {
        const quote = await records.createQuote({
          quote: req.body.quote,
          author: req.body.author,
        });
        res.json(quote);
      });
})

app.listen(3000, () => console.log('Quote API listening on port 3000!'));

1 Answer

Brandon White
seal-mask
MOD
.a{fill-rule:evenodd;}techdegree seal-36
Brandon White
Treehouse Moderator

Hi Muhammad,

If you could send a link to your complete repo, I could take a deeper look for you. But I’m thinking the issue might be that you’re not requiring a module to parse the body so that you can check the request body for data.

If you think that might be the issue then check out this link. It explains how to use one of the more popular body parsers for Node and Express.

https://www.npmjs.com/package/body-parser