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.
Lobiani Mania is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.