hj

hj
const express = require (‘express’)
const bodyParser =require(‘body-parser’)
const mongoose=require(‘mongoose’)
const student=require(‘./model’)
const app = express();
app.use(bodyParser.urlencoded({extended:true}))
app.use(bodyParser.json())
app.get(‘/’,async(req,res)=>{
    console.log(req)
    res.sendFile(__dirname+’/public/index.html’)
});
app.post(‘/add-student’,async(req,res)=>{
    const {Name,Age,Grade}= req.body
    try{
        const newStudent =new student({
            Name : Name,
            Age : Age,
            Grade:Grade
        })
        newStudent.save();
    }catch(error){
        console.log(‘error found’)
        res.status(500).json({error:’Error entering students details’})
    }
});
mongoose.connect(‘mongodb://127.0.0.1:27017/project’,{
    useNewUrlParser : true})
    .then(()=>{console.log(‘Successfully Connected’)})
    .catch(()=>{console.log(‘Not able to connect to the database’)})
app.listen(1233,()=>{
    console.log(“Server is listing to http://localhost:1233”)
})
Scroll to Top