skip to Main Content

I am new to backend and i was following codewithharry react js course. I did the same as he did but when i use to get request in thunderclient it gets stuck in processing. But when i use the link in chrome browser it works fine.

vscode thunderclient

can anyone help me?

my codes are:
index.js:

const connectDB = require('./db');
connectDB();
const express = require('express')
const app = express()
const port = 3000

app.use(express.json())
//Available routes
app.use('/api/auth', require('./routes/auth'))
app.use('/api/notes', require('./routes/notes'))

app.get('/', (req, res) => {
    res.send('Hello World!')
})

app.listen(port, () => {
    console.log(`Example app listening at http://localhost:${port}`)
})

db.js:

const mongoose = require('mongoose');
const mongoURI = "mongodb://127.0.0.1:27017/inotebookdb"

const connectDB = async () => {
    try {
        await mongoose.connect(mongoURI, { useNewUrlParser: true, useUnifiedTopology: true });
        console.log("successfully connected");
    } catch (err) {
        console.log(err);
    }
}


module.exports = connectDB;

2

Answers


  1. Your code looks right, yet there’s one improvement you can make: Move the app.listen call inside the connectDB capability to guarantee that the server begins solely after the information base association is effectively settled.

    update code :

    index.js file:

    const express = require('express');
    const app = express();
    const connectDB = require('./db');
    const port = 3000;
    
    app.use(express.json());
    
    app.use('/api/auth', require('./routes/auth'));
    app.use('/api/notes', require('./routes/notes'));
    
    app.get('/', (req, res) => {
        res.send('Hello World!');
    });
    
    connectDB().then(() => {
        app.listen(port, () => {
            console.log(`Example app listening at http://localhost:${port}`);
        });
    }).catch((err) => {
        console.error('Failed to connect to the database:', err);
    });
    

    db.js

    const mongoose = require('mongoose');
    const mongoURI = "mongodb://127.0.0.1:27017/inotebookdb";
    
    const connectDB = () => {
        return mongoose.connect(mongoURI, { useNewUrlParser: true, useUnifiedTopology: true })
            .then(() => {
                console.log("Successfully connected to the database");
            })
            .catch((err) => {
                console.log("Error connecting to the database:", err);
                throw err;
            });
    };
    
    module.exports = connectDB;
    

    In this rendition, the connectDB capability returns a commitment, permitting you to utilize .then, at that point() and .catch() while calling it in index.js. This guarantees that the server will just beginning tuning in after the data set association is laid out effectively.

    Furthermore, I’ve added some blunder taking care of in the data set association process, so you’ll see a message on the off chance that there’s an issue interfacing with the data set.

    Make sure to introduce the necessary conditions, like express, mongoose, and some other conditions you could have utilized in your courses and models. You can introduce them utilizing npm or yarn:

    npm install express mongoose
    

    With these changes, your backend ought to work accurately, and you ought to have the option to utilize ThunderClient or some other Programming interface client to make demands without stalling out in handling.

    Login or Signup to reply.
  2. Hi There is bug in VS Code 1.81.0. Downgrade to version 1.80.2

    More details here
    https://github.com/rangav/thunder-client-support/issues/1251#issuecomment-1666439365

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search