Skip to content

ps-19/Competitive-Programming

Folders and files

NameName
Last commit message
Last commit date

Latest commit

498ce3d · Feb 23, 2025

History

29 Commits
Mar 16, 2024
Apr 3, 2023
Mar 16, 2024
Apr 23, 2023
Dec 31, 2021
Mar 16, 2024
Apr 24, 2021
Feb 23, 2025
Dec 31, 2021
Nov 18, 2022
May 5, 2022
Feb 23, 2025
May 5, 2022
Mar 16, 2024
Jan 7, 2022
Aug 7, 2024
Jul 28, 2024
Apr 21, 2023
Feb 23, 2025

Repository files navigation

// Basics of starting a server using nodeJS


const express = require('express');
const app = express();
app.use(express.json());
const mongoose = require('mongoose');
const path = require('path');

//
app.use(express.urlencoded({ extended: false }));
const publicDirectoryPath = path.join(__dirname, './public');
app.use(express.static(publicDirectoryPath));
//


const PORT = 3000;

const DB="mongodb+srv://";


const userSchema = new mongoose.Schema({
    name: {
        type: String,
        required: true
    },
    email: {
        type: String,
        required: true
    }
})

var User = mongoose.model("demos", userSchema); // database schema


mongoose.connect(DB).then(() => {            
    console.log('Database Connected!');
}).catch((error)=>{
    console.log('Not Connected!'+error);
})

app.get('/', (req,res)=>{
    User.find({}).then((result)=>{
        res.send(result);
    }).catch((err)=>{
        res.send("Error found "+err);
    })
})

app.post('/', (req,res)=>{
    console.log(req.body);
    var name=req.body.name;
    var email=req.body.email;
    var user = new User({name,email});
    user.save().then(()=>{
        console.log("Saved !");
    }).catch((error)=>{
        console.log("Not saved !"+error);
    })
    res.send('Hello World');
})

app.put('/', (req,res)=>{
    res.send('Hello World');
    User.updateOne({email: "abc@abc.com"},{$set: {"name": "Rohit Sharma"}}).then((res)=>{
        console.log("Done! No error found: "+res.acknowledged);
    }).catch((error)=>{
        res.send("Error or Not Found " + error)
    })
})

app.delete('/', (req,res)=>{
    User.deleteOne({email: "abc@abc.com"}).then(()=>{
        res.send("Successfully Deleted");
    }).catch((err)=>{
        res.send(err);
    })
})


app.listen(PORT,()=>{
    console.log('Server is running on '+PORT);
})


About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages