Node Introduction

What is NodeJS..??

   Node is open source server environment.. it runs various platforms likes windows, unix, mac os etc. node uses js on the server.  node can generate dynamic page content. can create, open,read,write,delete, and close files on the server and can modify data in your database.

Setup Node js..

first you need to download and install node js https://nodejs.org/ 
then check your node version using node -v type in cmd

then you need to connect to port. for that you need to install express using npm install express --save
 after that you need to import react react-DOM express libraries
then you need to write following code to connect to port. your connection is successful print the message.

app.listen(3000, (err) =>{
  if(err){
     console.error(err);
     return;
      }
   console.log("Listen port 3000");
});

Before this you need to add details about project in package.json using npm init key word. then you can start project.

then i like to share about simple CRUD app using array.
first you need to define array in your class

const array =[];

then you need to create object class (user) and export the class

const User = function (pFirstName,pLastName,pId) {
    this.firstName=pFirstName;
    this.lastName=pLastName;
    //this.birthDay=pBDay;
    this.id=pId;
};

module.exports=User;

in main class you need ti import user class.

after that need to implement post get ,put(update), delete method etc

this is the method to post your data to array.
you can user postmen like software to import data

app.post('/add', (req,res) => {
   const user=new userObject(req.body.fName,req.body.lName,Date.now());
   userArr.push(user);
   res.status(200).send({message:"user add sucess",data:user});

});

this is the get all method. using this method can displays all the adding data.

app.get('/all', (req,res) => {
   try {
       res.status(200).send({data:userArr})
   } catch (e) {
       res.status(500).send({message:e})
   }
});

in this method implement to get the specific user by id.

app.get('/uid/:Id',(req,res)=> {
   try {
       const index = userArr.findIndex(x => x.id ==req.params.Id);
       if (index> -1){
           res.status(200).send(userArr[index]);
       } else {
           res.status(404).send({message:'in valide id'})
       }
   }catch (e) {
       res.status(500).send({message:e})
   }
});

this method develop for update user details using user id

app.put('/up/:Id', (req,res) => {
    const index = userArr.findIndex( ins => ins.id ==req.params.Id);
    userArr[index].firstName=req.body.fName;
    userArr[index].lastName=req.body.lName;

    if (index> -1){
        res.status(200).send(userArr[index]);
    } else {
        res.status(404).send({message:'in valide id'})
    }

    //res.status(200).send(userArr[index]);
});

in this method develop for delete user using id

app.delete('/user/:Id',(req,res)=>{
    try{
        const index = userArr.findIndex(x => x.id == req.params.Id);
        if (index > -1){
            const delUser = userArr.splice(index,1);
            res.status(200).send({message:'User remove from the array',removedObject:delUser,data:userArr});
        } else{
            res.status(404).send({message: 'Invalid Id Provided'});
        }
    }catch (e) {
        res.status(500).send({message:e});
    }
});


I think this blog helpful for start node app.

See you in next blog

thank you







Comments

Popular posts from this blog

MERN App

React installation