lesson 4

server.js

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465const express = require('express');
const dotenv = require('dotenv');
const connectDB = require('./config/db');
const Bootcamp = require('./models/Bootcamps');


// load env vars
dotenv.config({path: './config/config.env'});

const app = express();

const PORT = process.env.PORT || 5000;

const server = app.listen(
    PORT, 
    console.log(`Server running in ${process.env.NODE_ENV} mode on port ${PORT}`)
);

//connect to database
connectDB();
// Add new bootcamp
Bootcamp.create({name: "abc", description: "abc"});

// Get all bootcamps
async function getAllBootcamps(){
    const bootcamps= await Bootcamp.find();
    console.log(bootcamps);
}

getAllBootcamps();

//Get bootcamp by id
async function getBootcampById(id) {
    const bootcamp = await Bootcamp.findById(id);
    console.log(bootcamp);
}

getBootcampById("5dcc18194083310a78e53f2d");


// Update bootcamp by id
async function updateBootcampById(id, data) {
    const bootcamp = await Bootcamp.findByIdAndUpdate(id, data);
    getBootcampById(id);
}

updateBootcampById("5dcc18194083310a78e53f2d", {name: "Johnson Baby"})


// Delete bootcamp by id
async function deleteBootcampById(id) {
    await Bootcamp.findByIdAndDelete(id);
    getAllBootcamps();
}

deleteBootcampById("5dcc18194083310a78e53f2d");

// Query 
async function getBootcamp(query) {
    const bootcamp = await Bootcamp.findOne(query);
    console.log(bootcamp);
}

getBootcamp({name: "abc"});

models/Bootcamps.js

12345678910111213141516171819202122232425262728293031323334const mongoose = require('mongoose');

const BootcampSchema = new mongoose.Schema({
    name: {
        type: String,
        required: [true, 'Please add a name'], 
        unique: true,
        trim: true, 
        maxlength: [50, 'Name can not be more than 50 characters']
    },
    slug: String,
    description:{
        type: String,
        required: [true, 'Please add a description'], 
        maxlength: [500, 'Decription can not be more than 500 characters']
    },
    website: {
        type: String,
        match: [
            /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a0-zA-Z0-9()@:%_\+.~#?&//=]*)/,
        'Please use a valid URL with HTTp or HTTPS'
    ]
    },
    email: {
        type: String,
        match: [
            /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,
            'Please add a valid email'
        ]
    },

});

module.exports = mongoose.model('Bootcamp', BootcampSchema);

config/db.js

123456789101112131415161718192021222324const Mongoose = require ('mongoose');


const DB_HOST = '112.213.91.209';
const DB_PORT = 27070;
const DB_USERNAME = 'admin';
const DB_PASSWORD = 'Abc12345';
const DB_NAME = 'demo';

const DB_CONN_STRING = `mongodb://${DB_USERNAME}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_NAME}?authSource=admin`;

const connectDB = async () => {
    const conn = await Mongoose.connect(DB_CONN_STRING, {
        useNewUrlParser: true,
        useCreateIndex: true,
        useFindAndModify: false,
        useUnifiedTopology: true
    });

    console.log(`MongoDB Connected: ${conn.connection.host}`);

}

module.exports = connectDB;

config/config.env

12NODE_ENV=development
PORT=5000