MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017

I’m having problem using mongodb. I downloaded as said in docs for debian (i’m using kali linux) and first day it was fine. Everything i did worked well. Now for second boot up it started giving me issues nodemon would crash very soon after starting it and mongosh connection fails as well. This all started when i tried adding admin panel on my project and tried using passport.js. HELP

typeconst express = require('express');
const session = require('express-session'); 
const path = require('path');
const mongoose = require('mongoose');
const project = require('./models/product'); 
const methodOverride = require('method-override');
const passport = require('passport'); 
const LocalStrategy = require('passport-local').Strategy; 
const User = require('./models/user'); 
const Product = require('./models/product'); 

const categories = ['swimming', 'fashion', 'gym'];

mongoose.connect('mongodb://127.0.0.1:27017/project', {
    useNewUrlParser: true,
    useUnifiedTopology: true
});

const db = mongoose.connection;
db.on("error", console.error.bind(console, "connection error:"));
db.once("open", () => {
    console.log("Database connected");
});

const app = express();

app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));

app.use(express.urlencoded({ extended: true }));
app.use(methodOverride('_method'));

app.use(express.static(path.join(__dirname, 'public')));

// Session middleware
app.use(session({
    secret: 'secret',
    resave: false,
    saveUninitialized: false
}));

// Passport middleware
app.use(passport.initialize());
app.use(passport.session());

// Passport local strategy
passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());

// Middleware to check if user is admin
const isAdmin = (req, res, next) => {
    if (req.isAuthenticated() && req.user.isAdmin) {
        return next();
    }
    res.redirect('/login'); // Redirect non-admin users to login
};

app.get('/', (req, res) => {
    res.render('home');
});

app.get('/contact', (req, res) => {
    res.render('contact');
});

app.get('/gift', (req, res) => {
    res.render('gift');
});

// Products route (protected by isAdmin middleware)
app.get('/products', isAdmin, async (req, res) => {
    try {
        const products = await Product.find({});
        res.render('products/index', { products });
    } catch (err) {
        console.error(err);
        res.status(500).send('Server Error');
    }
});

// Authentication routes
app.get('/login', (req, res) => {
    res.render('login');
});

app.post('/login', passport.authenticate('local', {
    successRedirect: '/products',
    failureRedirect: '/login',
    failureFlash: true
}));

app.get('/logout', (req, res) => {
    req.logout();
    res.redirect('/');
});

app.get('/clothes', async (req, res) => {
    try {
        const products = await project.find({});
        res.render('clothes', { products });
    } catch (e) {
        res.status(500).send(e.message);
    }
});

app.get('/products', async (req, res) => {
    const { category } = req.query;
    try {
        if (category) {
            const products = await project.find({ category });
            res.render('products/index', { products, category });
        } else {
            const products = await project.find({});
            res.render('products/index', { products, category: 'All' });
        }
    } catch (error) {
        console.error(error);
        res.status(500).send('Server Error');
    }
});

app.get('/products/new', (req, res) => {
    res.render('products/new', { categories });
});

app.post('/products', async (req, res) => {
    try {
        const newProduct = new project(req.body);
        await newProduct.save();
        res.redirect(`/products/${newProduct._id}`);
    } catch (error) {
        console.error(error);
        res.status(500).send('Server Error');
    }
});

app.get('/products/:id', async (req, res) => {
    const { id } = req.params;
    try {
        const product = await project.findById(id);
        if (!product) {
            return res.status(404).render('404', { title: 'Product Not Found' });
        }
        res.render('products/details', { product });
    } catch (error) {
        console.error(error);
        res.status(500).send('Server Error');
    }
});

app.get('/products/:id/edit', async (req, res) => {
    const { id } = req.params;
    try {
        const product = await project.findById(id);
        if (!product) {
            return res.status(404).render('404', { title: 'Product Not Found' });
        }
        res.render('products/edit', { product, categories });
    } catch (error) {
        console.error(error);
        res.status(500).send('Server Error');
    }
});

app.put('/products/:id', async (req, res) => {
    const { id } = req.params;
    try {
        const product = await project.findByIdAndUpdate(id, req.body, { runValidators: true, new: true });
        res.redirect(`/products/${product._id}`);
    } catch (error) {
        console.error(error);
        res.status(500).send('Server Error');
    }
});

app.delete('/products/:id', async (req, res) => {
    const { id } = req.params;
    try {
        const deletedProduct = await project.findByIdAndDelete(id);
        res.redirect('/products');
    } catch (error) {
        console.error(error);
        res.status(500).send('Server Error');
    }
});

app.use((req, res) => {
    res.status(404).render('404', { title: 'Page Not Found' });
});

app.listen(3000, () => {
    console.log('Listening on port 3000');
}); here

I tried installing it again, restarting nodemon, checked config files (its good). There is old question about same issue here but non of those solutions worked for me. I literally changed kali version and set up new one and problem stays same. Nothing seems to work.

New contributor

Lobiani Mania is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật