Hi Steevej, so i replaced the localhost in connection string with 127.0.0.1. Here’s the code for server.js file which I am trying to run in cmd using ‘npm start’
const express = require('express');
const mongoose = require('mongoose');
const cors = require('cors');
const app = express();
const PORT = process.env.PORT || 5000;
app.use(cors());
app.use(express.json());
mongoose.connect('mongodb://localhost:127.0.0.1/ai-real-estate-friend');
const connection = mongoose.connection;
connection.once('open', () => {
console.log('MongoDB database connection established successfully');
});
app.listen(PORT, () => {
console.log(`Server is running on port: ${PORT}`);
});
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'client/build', 'index.html'));
});
const listingSchema = new mongoose.Schema({
title: String,
description: String,
address: String,
city: String,
state: String,
zip: String,
price: Number,
image: String
});
const Listing = mongoose.model('Listing', listingSchema);
app.get('/api', async (req, res) => {
try {
const listings = await Listing.find();
res.json(listings);
} catch (error) {
console.error(error);
res.status(500).send('Server error');
}
});