Routing in express (Node )

                                  ✔   Routing in express  (Node )


import express from 'express';
const app = express();
const port = process.env.PORT || '3000'
app.get('/',(req,res)=>{
    res.send("Hello Friends ");
});

app.all('/sab',(req,res)=>{
    res.send("Hello All  ");
});

app.all('/api/*',(req,res)=>{
    res.send("Page Not found");
})

//string pattern path.
app.get('/ab?cd',(req,res)=>{
    res.send('it showing abcd and acd');
    //it's showing abcd and acd .
});

//Regular expression
// app.get(/a/,(req,res)=>{
//     res.send('woking when type a with any data');
//     //not write with single '' quote with /a/
// });

app.get('/oncecallback/',(req,res)=>{
    res.send('First callBack is here.');
});

//This is a more than one callbacks.
app.get('/secondcb',(req,res,next)=>{
    console.log('first call back')
    next()
},(req,res)=>{
    res.send('This is a second Call backs');
})

//An Array of CallBacks
const cb1 = (req,res,next)=>{
    console.log('This is a first CallBacks');
    next()
}

const cb2 = (req,res,next)=>{
    console.log('This is a second CallBacks');
    next()
}

const cb3 = (req,res)=>{
    console.log('This is a Third CallBacks');
    res.send('This is an array of CallBacks.');
}

app.get('/thirdD',[cb1,cb2,cb3]);

//comibnaiton of array and call with next;



const numberone = (req,res,next)=>{
    console.log('first Callbacks');
    next();
}

const numbersecond = (req,res,next)=>{
    console.log('Second Callbacks');
    next();
}

app.get('/numberfour',[numberone,numbersecond],(req,res,next)=>{
    console.log('Third Callbacks');
    next()
},(req,res)=>{
    console.log('four Callbacks');
    res.send('This is a four CallBacks..');
});

//app route chaining when path is same
app.route('/students')
.get((req,res)=>{
    res.send("first get data ");
})
.post((req,res)=>{
    res.send("second post data");
})
.put((req,res)=>{
    res.send("Third putch data ");
});

//app route chaining when path is same 2
app.route('/studentss')
//yha first validation lga sakte hai.
.all((req,res,next)=>{
    console.log('this is firs work');
    next();
})
.get((req,res)=>{
    res.send("first get data ");
})
.post((req,res)=>{
    res.send("second post data");
})
.put((req,res)=>{
    res.send("Third putch data ");
});



app.listen(port,()=>{
    console.log("Server Connected Successfully.");
});

Comments

Popular posts from this blog

My Sql Query ..

Interview question laravel.