Intro To Mongo with Express JS

1st : npx express-generator
2nd : npm install
3rd : DEBUG=your app name:* npm start
npm install mongoose
npm install --save-dev nodemon
“start”: “DEBUG=mongodb-session:* nodemon node ./bin/www”
npm install body-parser
npm install dotenv
require('dotenv').config()
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json());
env file
creating a new database inside mongo
env file with the connection string
mongoose.connect(process.env.CONNECTION_STRING,{dbName: process.env.dbnam,useNewUrlParser:true,useUnifiedTopology:true,useCreateIndex:true,});
const {model, Schema} = require('mongoose');
const UserSchema=new Schema({})
const {model,Schema} = require('mongoose');const UserSchema=new Schema({username:{type: String,unique:true,trim:true,maxLength:100,required:true,},mail:{type:String,unique:true,trim:true,maxLength:100,required:true,lowercase:true,},password:{type:String,trim:true,required:true,minLength:8,},isSuperadmin:{type:Boolean,default:false,},isadmin:{type:Boolean,default:false,},phoneNumber:String,},{
collection:'users'
});
let UserModel= model('User',UserSchema);
module.exports=UserModel;
UsersController
Functions of UsersController
addusers(req, res, next) {
let body=req.body;
let user = new User(body);
user.save((err, res) => {
if (err) return next(err);
res.status(200).send(res);
})
}
// read all usersgetAllusers(req, res, next) {User.find({}, (err, response) => {if (err) return next(err);res.status(200).send(response);});}
// update userupdateuser(req, res, next) {let { id } = req.params;let body = req.body;User.updateOne({ _id: id }, {$set: body}, (err, response) => {if (err) return next(err);res.status(200).send(response);});}
// delete userdeleteuser(req, res, next) {
let { id } = req.params;
User.deleteOne({ _id: id }, (err, response) => {
if (err) return next(err);
res.status(200).send(response);
})
}
const User = require('../models/User')class UsersController {// read all users
getAllusers(req, res, next) {
User.find({}, (err, response) => {
if (err) return next(err);
res.status(200).send(response);
});
}
// add a useraddusers(req, res, next) {
let body = req.body;
let user = new User(body);
user.save((err, response) => {
if (err) return next(err);
res.status(200).send(response);
});
}// update user
updateuser(req, res, next) {
let { id } = req.params;
let body = req.body;
User.updateOne({ _id: id }, {
$set: body
}, (err, response) => {
if (err) return next(err);
res.status(200).send(response);
});
}
// delete user
deleteuser(req, res, next) {
let { id } = req.params;
User.deleteOne({ _id: id }, (err, response) => {
if (err) return next(err);
res.status(200).send(response);
})
}
}const usercontroller = new UsersController();
module.exports = usercontroller;
var express = require('express');
var router = express.Router();
var usersController = require('../Controller/usersController');
var usersRouter = require('./routes/users');
app.use('/users', usersRouter);
// get users listing.
router.get('/', usersController.getAllusers);
// add user
router.post('/', usersController.addusers);
// update user
router.put('/:id', usersController.updateuser);
// delete user
router.delete('/:id', usersController.deleteuser);
module.exports = router;
add
get
put
delete

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store