DEV Community

Cover image for mongo-dcur
Abhijit Meshram
Abhijit Meshram

Posted on

mongo-dcur

{
"name": "backend",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon server.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": "",
"dependencies": {
"body-parser": "^1.20.3",
"cors": "^2.8.5",
"dotenv": "^16.4.7",
"express": "^4.21.2",
"mongodb": "^6.11.0",
"mongoose": "^8.8.4",
"nodemon": "^3.1.7"
}
}

const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const connectDatabase = require('./dbconnection/dbconnection');
const routes = require('./routes/studentroutes');

const app = express();

// Middleware
app.use(bodyParser.json());
app.use(cors());

// Database Connection
connectDatabase();

// Routes
app.use('/api', routes);

// Start Server
app.listen(5001, () => {
console.log('Server started on port 5001');
});

const express = require('express');
const { getUsers, createUser, updateUser, deleteUser } = require('../controller/studentcontroller');
const router = express.Router();

router.get('/users', getUsers);
router.post('/user', createUser);
router.put('/user/:id', updateUser);
router.delete('/user/:id', deleteUser);

module.exports = router;

`const mongoose = require('mongoose');

const userSchema = new mongoose.Schema({
firstname: {
type: String,
required: true, // Add validation if necessary
},
lastname: {
type: String,
required: true,
},
email: {
type: String,
required: true,
unique: true, // Ensures no duplicate emails
},
mobile: {
type: String,
required: true,
},
city: {
type: String,
},
gender: {
type: String,
enum: ['Male', 'Female', 'Other'], // Limit to specific values if needed
},
skills: {
type: String,
},
dob: {
type: Date,
},
photo: {
type: String,
default: '', // Default value for the photo field
},
}, { timestamps: true }); // Automatically adds createdAt and updatedAt fields

module.exports = mongoose.model('User', userSchema);

const mongoose = require('mongoose');

const connectDatabase = async () => {
try {
await mongoose.connect('mongodb://localhost:27017/userdata', {
useNewUrlParser: true,
useUnifiedTopology: true,
});
console.log("Database connected successfully");
} catch (error) {
console.error("Error connecting to the database:", error);
}
};

module.exports = connectDatabase;`


`const User = require('../components/userSchema');

const getUsers = async (req, res) => {
try {
const users = await User.find({});
res.send(users);
} catch (error) {
console.error(error);
res.status(500).send(error);
}
};

const createUser = async (req, res) => {
try {
const user = new User(req.body);
await user.save();
res.status(201).json(user);
} catch (error) {
console.error(error);
res.status(500).send(error);
}
};

const updateUser = async (req, res) => {
const { id } = req.params;

try {
const user = await User.findByIdAndUpdate(id, req.body, { new: true });
res.send(user);
} catch (error) {
console.error(error);
res.status(500).send(error);
}
};

const deleteUser = async (req, res) => {
const { id } = req.params;

try {
const user = await User.findByIdAndDelete(id);
res.send(user);
} catch (error) {
console.error(error);
res.status(500).send(error);
}
};

module.exports = {
getUsers,
createUser,
updateUser,
deleteUser,
};
`

Top comments (0)