Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I can't get localhost:3001/api/notes to display anything

So my server.js code is as follows:

const express = require('express');

const PORT = process.env.PORT || 3001;
const app = express();
const { notes } = require('./db/db');

app.get('/api/notes', (req, res) => {
    const result = notes;
    res.json(result);
});

app.listen(PORT, () => {
    console.log(`API server now on port ${PORT}!`);
});

The db.json file is as follows:

[
    {
        "title":"Test",
        "text":"Test"
    }
]

After running npm start in terminal and going to localhost:3001/api/notes it supposed to show the contents of db.json, but it is just showing a blank page. Can anyone tell me what I am doing wrong?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

When i replaced res.json(result) with res.send('Hello') it displayed the "Hello" just fine which leads me to believe there is a problem with connecting db.json to server.js

Help Please?

>Solution :

Unless there is a notes property of your db.json file there is no need to surround your notes variable during its declaration.

Instead use:

const notes = require('./db/db');

If you use

const { notes } = require('./db/db');

Its the same as trying to access the notes property of the json file, which youve shown none so I’ve assumed its blank.

You can read more on destructuring assignments here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading